简体   繁体   中英

Filter classes in ant when using pitest

Let us consider this basic ant+pitest example: https://github.com/hcoles/pitest-ant-example

The classpath to the test classes is defined as follow:

<!-- 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>

Now, lets say I want to change it in order to filter out some classes. For example I want to include only the test classes whose name starts with "Partially":

<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>

Unfortunately this solution gives me the following error:

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

The file /root/pitest-ant-example/build/test-classes/com/example/PartiallyTestedTest$1.class does actually exist.

What am I doing wrong ? How can I filter the test classes I want to use ?

When you change from the simple 'pathelement' to the more complex 'fileset', the you change also the time when the classpath is evaluatet.

The simple 'pathelement' just tells ant to include a directory, but did not look into that directory when the path is defined.

The more complex fileset is evaluated in that moment when the path is first used. In that moment the inner class (that one that contains a $) does not exists. So the class is missing at runtime.

You have to use a trick so that the path is evaluated at the right time, after the inner class has been generated.

You do not need to remove things from the classpath to select tests to run or classes to mutate. Pitest provides its own filters.

In recent versions these are

  • targetClasses
  • targetTests
  • excludedClasses
  • excludedTests

Each accepts a list of globs.

Ant plugin usage is documented here http://pitest.org/quickstart/ant/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM