gpt4 book ai didi

java - 使用 jacoco 离线检测的 gradle 每次测试的覆盖率 - java.lang.IllegalStateException : JaCoCo agent not started

转载 作者:行者123 更新时间:2023-12-03 02:43:22 25 4
gpt4 key购买 nike

我正在尝试使用 gradle+jacoco 对每个测试进行覆盖。
想法是实现 org.gradle.api.tasks.testing.TestListener ,并在常规事件(测试开始和测试结束)和转储覆盖率时重置 session ID。
(演示问题的整个项目在 https://github.com/jayanmn/gradle-coverage-per-test-debug/tree/main )
检测是通过 jacoco-offline 检测使用 gradle 中的 ant 任务完成的。 (离线,因为测试环境包括一些测试 - 有点集成类型)。

buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath group: 'org.jacoco', name: 'org.jacoco.agent', version: '0.8.5', classifier: 'runtime'
classpath group: 'org.jacoco', name: 'org.jacoco.core', version: '0.8.5'
}
}
依赖部分如下所示
    dependencies {
//offline instrument via ant task
jacocoAnt group: 'org.jacoco', name: 'org.jacoco.ant', version: '0.8.5', classifier: 'nodeps'

//the jacoco runtime private for RT.getAgent()
jacocoRuntime group: 'org.jacoco', name: 'org.jacoco.agent', version: '0.8.5', classifier: 'runtime'

testImplementation 'junit:junit:4.12'
}
运行测试(sampletest)
task sampletest(type: Test) {
ignoreFailures = true
//jars used every step is same.
println classpath.asPath

forkEvery 1
maxParallelForks 1

doFirst {
println "org.jacoco.agent.rt.RT loaded from " + RT.class.getProtectionDomain().getCodeSource()

systemProperty 'jacoco-agent.destfile', buildDir.path + '/jacoco/firsttests.exec'
classpath = files(instrument.outputDir) + classpath + configurations.jacocoRuntime

}
beforeTest { TestDescriptor descriptor ->
println "org.jacoco.agent.rt.RT loaded from " + RT.class.getProtectionDomain().getCodeSource()
}

afterTest { TestDescriptor descriptor ->
//get agent will fail with Agent not started.
systemProperty 'jacoco-agent.destfile', buildDir.path + '/jacoco/aftertests.exec'

//agent not started error
def agent = RT.getAgent()
agent.sessionId = descriptor.name
agent.dump(true)

}
}
因为某些原因
> java.lang.IllegalStateException: JaCoCo agent not started.

* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':sampletest'.
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.lambda$executeIfValid$1(ExecuteActionsTaskExecuter.java:207)
at org.gradle.internal.Try$Failure.ifSuccessfulOrElse(Try.java:263)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeIfValid(ExecuteActionsTaskExecuter.java:205)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:186)
at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:114)
at org.gradle.api.internal.tasks.execution.FinalizePropertiesTaskExecuter.execute(FinalizePropertiesTaskExecuter.java:46)
at org.gradle.api.internal.tasks.execution.ResolveTaskExecutionModeExecuter.execute(ResolveTaskExecutionModeExecuter.java:62)
at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:57)
at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:56)
at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:36)
at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.executeTask(EventFiringTaskExecuter.java:77)
at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.call(EventFiringTaskExecuter.java:55)
at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.call(EventFiringTaskExecuter.java:52)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$CallableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:409)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$CallableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:399)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$1.execute(DefaultBuildOperationExecutor.java:157)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:242)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:150)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.call(DefaultBuildOperationExecutor.java:94)
at org.gradle.internal.operations.DelegatingBuildOperationExecutor.call(DelegatingBuildOperationExecutor.java:36)
at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter.execute(EventFiringTaskExecuter.java:52)
at org.gradle.execution.plan.LocalTaskNodeExecutor.execute(LocalTaskNodeExecutor.java:41)
at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$InvokeNodeExecutorsAction.execute(DefaultTaskExecutionGraph.java:356)
at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$InvokeNodeExecutorsAction.execute(DefaultTaskExecutionGraph.java:343)
at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$BuildOperationAwareExecutionAction.execute(DefaultTaskExecutionGraph.java:336)
at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$BuildOperationAwareExecutionAction.execute(DefaultTaskExecutionGraph.java:322)
at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.lambda$run$0(DefaultPlanExecutor.java:127)
at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.execute(DefaultPlanExecutor.java:191)
at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.executeNextNode(DefaultPlanExecutor.java:182)
at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.run(DefaultPlanExecutor.java:124)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
Caused by: java.lang.IllegalStateException: JaCoCo agent not started.
at org.jacoco.agent.rt.internal_43f5073.Agent.getInstance(Agent.java:77)
at org.jacoco.agent.rt.RT.getAgent(RT.java:33)
at org.jacoco.agent.rt.RT$getAgent.call(Unknown Source)
at build_q0s35v9r0an7s8a7ayxelgpr$_run_closure6$_closure13.doCall(C:\repos\number-utils\build.gradle:91)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.gradle.listener.ClosureBackedMethodInvocationDispatch.dispatch(ClosureBackedMethodInvocationDispatch.java:41)
at org.gradle.listener.ClosureBackedMethodInvocationDispatch.dispatch(ClosureBackedMethodInvocationDispatch.java:25)
at org.gradle.internal.event.AbstractBroadcastDispatch.dispatch(AbstractBroadcastDispatch.java:42)
at org.gradle.internal.event.BroadcastDispatch$SingletonDispatch.dispatch(BroadcastDispatch.java:245)
at org.gradle.internal.event.BroadcastDispatch$SingletonDispatch.dispatch(BroadcastDispatch.java:157)
at org.gradle.internal.event.AbstractBroadcastDispatch.dispatch(AbstractBroadcastDispatch.java:58)
at org.gradle.internal.event.BroadcastDispatch$CompositeDispatch.dispatch(BroadcastDispatch.java:346)
at org.gradle.internal.event.BroadcastDispatch$CompositeDispatch.dispatch(BroadcastDispatch.java:249)
at org.gradle.internal.event.ListenerBroadcast.dispatch(ListenerBroadcast.java:141)
at org.gradle.internal.event.ListenerBroadcast.dispatch(ListenerBroadcast.java:37)
at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
at com.sun.proxy.$Proxy69.afterTest(Unknown Source)
at org.gradle.api.internal.tasks.testing.results.TestListenerAdapter.completed(TestListenerAdapter.java:50)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.event.AbstractBroadcastDispatch.dispatch(AbstractBroadcastDispatch.java:42)
at org.gradle.internal.event.BroadcastDispatch$SingletonDispatch.dispatch(BroadcastDispatch.java:245)
at org.gradle.internal.event.BroadcastDispatch$SingletonDispatch.dispatch(BroadcastDispatch.java:157)
at org.gradle.internal.event.AbstractBroadcastDispatch.dispatch(AbstractBroadcastDispatch.java:58)
at org.gradle.internal.event.BroadcastDispatch$CompositeDispatch.dispatch(BroadcastDispatch.java:346)
at org.gradle.internal.event.BroadcastDispatch$CompositeDispatch.dispatch(BroadcastDispatch.java:249)
at org.gradle.internal.event.ListenerBroadcast.dispatch(ListenerBroadcast.java:141)
at org.gradle.internal.event.ListenerBroadcast.dispatch(ListenerBroadcast.java:37)
at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
at com.sun.proxy.$Proxy71.completed(Unknown Source)
at org.gradle.api.internal.tasks.testing.results.StateTrackingTestResultProcessor.completed(StateTrackingTestResultProcessor.java:96)
at org.gradle.api.internal.tasks.testing.results.AttachParentTestResultProcessor.completed(AttachParentTestResultProcessor.java:56)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)

问题可能出在“buildscript”和测试块的类加载器上?添加 jacoco-agent-runtime 的正确方法是什么?

最佳答案

afterTest org.gradle.api.tasks.testing.TestListener在用于 Gradle 的 VM 中由 Gradle 执行,但不在用于使用 JaCoCo 进行测试的 VM 中执行。
对于证明:
src/test/java/ExampleTest.java

public class ExampleTest {
@org.junit.Test
public void test() {
System.out.println(java.lang.management.ManagementFactory.getRuntimeMXBean().getName());
}
}
build.gradle
plugins {
id "java"
}

repositories {
mavenCentral()
}

dependencies {
testImplementation "junit:junit:4.12"
}

test {
testLogging.showStandardStreams = true
afterTest {
System.println(java.lang.management.ManagementFactory.getRuntimeMXBean().getName())
}
}
命令的执行 gradle build会产生类似的东西
> Task :test

ExampleTest > test STANDARD_OUT
33542@Godin.local
33018@Godin.local
两个数字 3354233018是 Java 进程的 ID,正如您所见,它们是不同的。
org.jacoco.agent.rt.RT只能用于与在同一 VM 中运行的 JaCoCo 进行通信。
要与另一个 VM 中的 JaCoCo 通信,您可以使用代理 output模式 tcpclienttcpserver ,或通过 JMX 使用代理选项 jmx=true - 见 https://www.jacoco.org/jacoco/trunk/doc/agent.html以及可用于离线检测的相同选项 - 参见 https://www.eclemma.org/jacoco/trunk/doc/offline.html

Idea is to have an implementation of org.gradle.api.tasks.testing.TestListener, and reset session-id at regular events (test start and test end) and dump coverage.


JUnit 4 具有 org.junit.runner.notification.RunListener它在 VM 内执行以供 JUnit 进行测试,其实现示例与您的想法类似,可以在 https://github.com/SonarSource/sonar-java/blob/5.14.0.18788/sonar-jacoco-listeners/src/main/java/org/sonar/java/jacoco/JUnitListener.java 中找到。
不幸的是根据 https://github.com/gradle/gradle/issues/1330
似乎到目前为止,在 Gradle 中没有简单的方法来配置 JUnit 4 以使用 org.junit.runner.notification.RunListener ,但是似乎可以配置 JUnit 5 以使用其 org.junit.platform.launcher.TestExecutionListener .

关于java - 使用 jacoco 离线检测的 gradle 每次测试的覆盖率 - java.lang.IllegalStateException : JaCoCo agent not started,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64402342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com