我如何 运行 一个 pitest ant 脚本

How do I run a pitest ant script

我正在尝试为 运行 pitest 创建一个 ant 脚本,以便能够自动化我的变异测试。我收到错误:

Could not find or load main class 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)

我不知道这是好是坏,但希望它能帮助您找到问题。

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

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

我建议您从这里开始并对其进行编辑,直到您的代码库构建工作正常。

我能看到的一个区别是您没有在最糟糕的路径中包含 junit。

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

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

我相信您的大部分问题是您正在尝试使用 Eclipse 生成的 build.xml 文件,该文件不包含变异测试目标,以及您为解决此问题而添加的目标有一些错误。

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

  1. 您将文件分为两个源目录 "src" 和 "test"
  2. src 和 test 文件夹都包含包 "pbook"
  3. 您将测试的名称更改为以 "Test" 结尾,而不是以
  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>