5

我正在使用 Jacoco Junit 任务并尝试了以下操作:

    <!-- run the junit tests -->
    <echo>DEBUG: $${junit.fork} = "${junit.fork}"</echo>
    <jacoco:coverage
        destfile="${target.dir}/jacoco.exec"
        append="false">
        <junit fork="${junit.fork}" includeAntRuntime="true">
            <classpath>
                <pathelement path="${main.destdir}"/>
                <pathelement path="${test.destdir}"/>
            </classpath>
            <classpath refid="test.classpath"/>
            <formatter type="${junit.formatter.type}"/>
            <batchtest      todir="${junit.batchtest.todir}">
                <fileset dir="${test.destdir}" />
            </batchtest>
        </junit>
    </jacoco:coverage>

并得到以下信息:

test:
     [echo] DEBUG: ${junit.fork} = "true"
[jacoco:coverage] Enhancing junit with coverage

BUILD FAILED
D:\build.xml:233: Coverage can only be applied on a forked VM

Total time: 6 seconds

D:\>

如您所见,${junit.fork}属性设置为true,我在<junit fork="${junit.fork}"/>.

但是,我没有使用该属性,而是简单地设置<junit fork="true">了它,它可以正常工作:

    <jacoco:coverage
        destfile="${target.dir}/jacoco.exec"
        append="false">
        <junit fork="true" includeAntRuntime="true">
            <classpath>
                <pathelement path="${main.destdir}"/>
                <pathelement path="${test.destdir}"/>
            </classpath>
            <classpath refid="test.classpath"/>
            <formatter type="${junit.formatter.type}"/>
            <batchtest      todir="${junit.batchtest.todir}">
                <fileset dir="${test.destdir}" />
            </batchtest>
        </junit>
    </jacoco:coverage>

当我使用该属性时,我已经运行ant -d test以验证 Java JVM 是否正在分叉。${junit.fork}

为什么 JaCoCo 坚持不分叉 JUnit 测试,除非我将fork参数设置为字符串true而不是设置为 equals 的属性true

4

1 回答 1

3

根据Jacoco 覆盖任务源代码

...
public void enhanceTask(final Task task) {
    final RuntimeConfigurable configurableWrapper = task.getRuntimeConfigurableWrapper();

    final String forkValue = (String) configurableWrapper.getAttributeMap().get("fork");
    if (!Project.toBoolean(forkValue)) {
        throw new BuildException("Coverage can only be applied on a forked VM", getLocation());
    }
    addJvmArgs(task);
}
...

从可配置包装器中读取 forkValue。使用由 调用${junit.fork}的方法评估属性映射值(如) 。而当你继续查看 Jacoco 源代码时: RuntimeConfigurable.maybeConfigureTask.mayBeConfigure

...
public void addTask(final Task task) {
    if (childTask != null) {
        throw new BuildException("Only one child task can be supplied to the coverge task", getLocation());
    }
    this.childTask = task;
    final String subTaskTypeName = task.getTaskType();
    final TaskEnhancer enhancer = findEnhancerForTask(subTaskTypeName);
    if (enhancer == null) {
        throw new BuildException(format("%s is not a valid child of the coverage task", subTaskTypeName), getLocation());
}
    if (isEnabled()) {
        log(format("Enhancing %s with coverage", childTask.getTaskName()));
        enhancer.enhanceTask(task);
    }
    task.maybeConfigure();
}
...

此方法 ( mayBeConfigure) 在任务已增强 ( enhancer.enhanceTask(task);) 后调用。我猜这是你的问题和一个错误......我们应该将它报告给 jacoco 错误跟踪器。

雅可可问题

于 2012-08-21T10:20:51.703 回答