让我们考虑这个基本的蚂蚁+pitest示例:https://github.com/hcoles/pitest-ant-example
测试类的类路径定义如下:
<!-- classpath for compiling and testing the code. Note it does not include pitest and it's dependencies -->
<path id="test.path">
<pathelement location="${classOutputDir}/classes" />
<pathelement location="${classOutputDir}/test-classes" />
<pathelement location="lib/junit-4.9.jar" />
</path>
现在,假设我想更改它以过滤掉一些类。例如,我只想包含名称以"部分"开头的测试类:
<path id="test.path">
<pathelement location="${classOutputDir}/classes" />
<fileset dir="${classOutputDir}/test-classes">
<include name="**/Partially*.class" />
<exclude name="**/ExcludedTest*.class" />
</fileset>
<pathelement location="lib/junit-4.9.jar" />
</path>
不幸的是,此解决方案给了我以下错误:
pit:
[pitest] Exception in thread "main" org.pitest.util.PitError: error in opening zip file (/root/pitest-ant-example/build/test-classes/com/example/PartiallyTestedTest$1.class)
[pitest]
[pitest] Please copy and paste the information and the complete stacktrace below when reporting an issue
[pitest] VM : Java HotSpot(TM) 64-Bit Server VM
[pitest] Vendor : Oracle Corporation
[pitest] Version : 25.161-b12
[pitest] Uptime : 354
[pitest] Input ->
[pitest] BootClassPathSupported : true
[pitest]
[pitest] at org.pitest.util.Unchecked.translateCheckedException(Unchecked.java:25)
[pitest] at org.pitest.classpath.ArchiveClassPathRoot.getRoot(ArchiveClassPathRoot.java:120)
[pitest] at org.pitest.classpath.ArchiveClassPathRoot.getData(ArchiveClassPathRoot.java:46)
[pitest] at org.pitest.classpath.CompoundClassPathRoot.getData(CompoundClassPathRoot.java:27)
[pitest] at org.pitest.classpath.ClassPath.getClassData(ClassPath.java:97)
[pitest] at org.pitest.classpath.ClassPathByteArraySource.getBytes(ClassPathByteArraySource.java:41)
[pitest] at org.pitest.classinfo.Repository.querySource(Repository.java:82)
[pitest] at org.pitest.classinfo.Repository.nameToClassInfo(Repository.java:68)
[pitest] at org.pitest.classinfo.Repository.fetchClass(Repository.java:60)
[pitest] at org.pitest.mutationtest.config.ConfigurationFactory.createConfiguration(ConfigurationFactory.java:52)
[pitest] at org.pitest.mutationtest.config.LegacyTestFrameworkPlugin.createTestFrameworkConfiguration(LegacyTestFrameworkPlugin.java:38)
[pitest] at org.pitest.mutationtest.config.SettingsFactory.getTestFrameworkPlugin(SettingsFactory.java:133)
[pitest] at org.pitest.mutationtest.config.SettingsFactory.createCoverageOptions(SettingsFactory.java:142)
[pitest] at org.pitest.mutationtest.tooling.EntryPoint.execute(EntryPoint.java:80)
[pitest] at org.pitest.mutationtest.tooling.EntryPoint.execute(EntryPoint.java:45)
[pitest] at org.pitest.mutationtest.commandline.MutationCoverageReport.runReport(MutationCoverageReport.java:87)
[pitest] at org.pitest.mutationtest.commandline.MutationCoverageReport.main(MutationCoverageReport.java:45)
[pitest] Caused by: java.util.zip.ZipException: error in opening zip file
[pitest] at java.util.zip.ZipFile.open(Native Method)
[pitest] at java.util.zip.ZipFile.<init>(ZipFile.java:225)
[pitest] at java.util.zip.ZipFile.<init>(ZipFile.java:155)
[pitest] at java.util.zip.ZipFile.<init>(ZipFile.java:169)
[pitest] at org.pitest.classpath.ArchiveClassPathRoot.getRoot(ArchiveClassPathRoot.java:118)
[pitest] ... 15 more
BUILD FAILED
/root/pitest-ant-example/build.xml:109: /root/pitest-ant-example/build.xml:109: Java returned: 1
Total time: 2 seconds
文件/root/pitest-ant-example/build/test-classes/com/example/PartiallyTestedTest$1.class
确实存在。
我做错了什么?如何过滤要使用的测试类?
当您从简单的"pathelement"更改为更复杂的"文件集"时,您也会更改类路径的计算时间。
简单的"pathelement"只是告诉ant包含一个目录,但在定义路径时没有查看该目录。
更复杂的文件集在首次使用路径的那一刻进行评估。在那一刻,内部类(包含 $ 的类(不存在。因此,该类在运行时丢失。
您必须使用一个技巧,以便在生成内部类后在正确的时间评估路径。
您无需从类路径中删除内容即可选择要运行的测试或要更改的类。Pitest提供自己的过滤器。
在最近的版本中,这些是
- 目标类
- 目标测试
- 排除类
- 排除的测试
每个都接受一个球体列表。
蚂蚁插件的使用记录在这里 http://pitest.org/quickstart/ant/