如何运行pitest ant脚本

时间:2015-02-18 23:07:54

标签: java ant pitest

我试图创建一个运行pitest的ant脚本,以便能够自动进行突变测试。我收到了错误:

  

无法找到或加载主类org.pitest.mutationtest.commandline.MutationCoverageReport

这是我的MutationTest.xml ant脚本

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="mutationCoverage" name="PhoneBook">
    <property name="ECLIPSE_HOME" value="C:/Program Files/eclipse/"/>
    <path id="JUnit 4.libraryclasspath">
        <pathelement location="${ECLIPSE_HOME}plugins/org.junit_4.11.0.v201303080030/junit.jar"/>
        <pathelement location="${ECLIPSE_HOME}plugins/org.hamcrest.core_1.3.0.v201303031735.jar"/>
    </path>
    <path id="PhoneBook.classpath">
        <pathelement location="bin"/>
        <path refid="JUnit 4.libraryclasspath"/>
    </path>
    <path id="pit.path">
        <pathelement location="lib/pitest-1.1.4.jar" />
        <pathelement location="lib/pitest-ant-1.1.4.jar" />
    </path>

    <taskdef name="pitest" classname="org.pitest.ant.PitestTask" classpathref="pit.path" />

    <target name="mutationCoverage">
        <pitest
            pitClasspath="PhoneBook.path"
            classPath="PhoneBook.path"
            targetClasses="pbook.*"
            targetTests="pbook.*"
            reportDir="MutationReports"
            sourceDir="src"/>
    </target>
</project>

导致此错误的原因是什么,我该如何解决?

修改:我将pitClasspath="PhoneBook.path"更改为pitClasspath="pit.path",现在我遇到了新的错误:

[pitest] Exception in thread "main" org.pitest.util.PitError: Unable to load class content for org.pitest.boot.HotSwapAgent
[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.25-b02
[pitest] Uptime : 370
[pitest] Input -> 
[pitest] BootClassPathSupported : true
[pitest]    at org.pitest.mutationtest.tooling.JarCreatingJarFinder.classBytes(JarCreatingJarFinder.java:124)
[pitest]    at org.pitest.mutationtest.tooling.JarCreatingJarFinder.addClass(JarCreatingJarFinder.java:113)
[pitest]    at org.pitest.mutationtest.tooling.JarCreatingJarFinder.createJarFromClassPathResources(JarCreatingJarFinder.java:98)
[pitest]    at org.pitest.mutationtest.tooling.JarCreatingJarFinder.createJar(JarCreatingJarFinder.java:74)
[pitest]    at org.pitest.mutationtest.tooling.JarCreatingJarFinder.getJarLocation(JarCreatingJarFinder.java:63)
[pitest]    at org.pitest.mutationtest.tooling.EntryPoint.execute(EntryPoint.java:70)
[pitest]    at org.pitest.mutationtest.tooling.EntryPoint.execute(EntryPoint.java:43)
[pitest]    at org.pitest.mutationtest.commandline.MutationCoverageReport.runReport(MutationCoverageReport.java:72)
[pitest]    at org.pitest.mutationtest.commandline.MutationCoverageReport.main(MutationCoverageReport.java:43)

我不知道这是好还是坏,但希望它有助于找到问题。

2 个答案:

答案 0 :(得分:1)

提供了一个pitest ant构建的工作示例

https://github.com/hcoles/pitest-ant-example

我建议你从这开始编辑它,直到你的代码库有一个有效的构建。

我可以看到的一个区别是你没有将junit包括在最陡峭的道路上。

你的构建看起来有点奇怪,因为它似乎与eclipse有关。如果您从IDE运行,为什么不使用eclipse插件?

此外,如果您不依赖于Ant,您可能需要将maven视为替代方案。

答案 1 :(得分:0)

我相信你的很多问题是你正在尝试使用Eclipse生成的build.xml文件,该文件不包含变异测试目标,而你为补救这个目标而添加的目标有一些错误。

我建议从项目here开始,尝试了解它的工作原理,然后更改build.xml文件以满足您的需求。 但是,如果这不起作用,从other question判断,以下build.xml应该在以下情况下起作用:

  1. 您将文件分成两个源目录“src”和“test”
  2. src和test文件夹都包含“pbook”包
  3. 您将测试的名称更改为以“测试”结尾而不是从中开始
  4. <?xml version="1.0" encoding="UTF-8"?>
    
    <project name="Phonebook">
    
        <property name="classOutputDir" value="build" />
    
            <!-- classpath for pitest and any plugins -->
        <path id="pitest.path">
                    <!-- must currently include the test library on the tool classpath. this will be fixed in a future version-->
               <pathelement location="lib/junit-4.9.jar" />
            <pathelement location="lib/pitest-0.33.jar" />
            <pathelement location="lib/pitest-ant-0.33.jar" />
        </path>
    
        <taskdef name="pitest" classname="org.pitest.ant.PitestTask" classpathref="pitest.path" />
    
        <target name="clean">
            <delete dir="${classOutputDir}" />
        </target>
    
        <target name="compile" depends="clean">
            <mkdir dir="${classOutputDir}/classes" />
            <!-- Essential that line numbers and filenames are included in order for PIT to work -->
            <javac srcdir="src" includeantruntime="false" debug="true" debuglevel="source,lines" destdir="${classOutputDir}/classes" />
        </target>
    
       <!-- 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>
    
        <target name="test" depends="compile">
            <mkdir dir="${classOutputDir}/test-result" />
            <mkdir dir="${classOutputDir}/test-classes" />
            <javac includeantruntime="false" srcdir="test" destdir="${classOutputDir}/test-classes">
                <classpath refid="test.path" />
            </javac>
    
            <junit>
                <classpath refid="test.path" />
                <batchtest todir="${classOutputDir}/test-result">
                    <!-- set test classes -->
                    <fileset dir="test">
                        <include name="**/*Test.java" />
                    </fileset>
                    <formatter type="xml" />
                </batchtest>
            </junit>
    
            <junitreport todir="${classOutputDir}/test-result">
                <fileset dir="${classOutputDir}/test-result">
                    <include name="TEST-*.xml" />
                </fileset>
                <report format="frames" todir="${classOutputDir}/test-result/report" />
            </junitreport>
    
        </target>
        
        <!-- run pitest. note that the filters for tests and classes refer to package/class names, not source file named -->
        <target name="pit" depends="test">
            <path id="mutation.path">
                <path refid="pitest.path"/>
                <path refid="test.path"/>
            </path>
            <pitest pitClasspath="pitest.path" threads="2" classPath="mutation.path" targetTests="pbook.*" targetClasses="pbook.*" reportDir="pitReports" sourceDir="src" />
        </target>
    
    </project>

相关问题