Java 如何通过 Gradle 测试任务在我的 JUnit 上启用调试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22760752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:45:11  来源:igfitidea点击:

How to enable debug on my JUnit through Gradle test task

javaeclipseunit-testingtestinggradle

提问by mefi

I get into trouble while I try to run my JUnit test through gradle test task. While I run test in eclipse directly with Run As -> JUnit test, everything is ok, test succeeds. But through test task, test always fails. Probably some trouble with encoding of my resource txt file. So I would like to enable debug while I am launching test with gradle

我在尝试通过 gradle 测试任务运行 JUnit 测试时遇到了麻烦。虽然我直接在 Eclipse 中使用 Run As -> JUnit test 运行测试,但一切正常,测试成功。但是通过测试任务,测试总是失败。我的资源 txt 文件的编码可能有些问题。所以我想在使用 gradle 启动测试时启用调试

in build.gradle, my test task now looks like:

在 build.gradle 中,我的测试任务现在看起来像:

test {
    tasks.withType(Compile) {
        options.encoding = 'UTF-8'
    }
}

So what should I do for enable debug? I run gradle tasks from Gradle panel in Eclipse, not from console. Thanks!

那么我应该怎么做才能启用调试?我从 Eclipse 中的 Gradle 面板运行 gradle 任务,而不是从控制台。谢谢!

采纳答案by Sergey

To debug tests the following argument should be used: --debug-jvm

要调试测试,应使用以下参数: --debug-jvm

For example: gradle test --debug-jvm
Gradle will suspend execution right before running tests and wait for debugger connection on port 5005.

例如:gradle test --debug-jvm
Gradle 将在运行测试之前暂停执行并等待端口 5005 上的调试器连接。

For executing only specific tests see https://docs.gradle.org/current/userguide/java_testing.html#simple_name_pattern

要仅执行特定测试,请参阅https://docs.gradle.org/current/userguide/java_testing.html#simple_name_pattern

回答by Peter Niederwieser

As explained under 23.12. Testin the Gradle User Guide, executing gradle test -Dtest.single=MyTestClass -Dtest.debugwill suspend the test JVM upon start, and allows to connect an external debugger (such as the Eclipse debugger) on port 5005.

如下说明23.12. Test摇篮用户指南,执行gradle test -Dtest.single=MyTestClass -Dtest.debug在启动时将暂停测试JVM,并允许外部调试器(如Eclipse调试器)在端口5005连接。

回答by niken

Putting this here as --debug-jvmdid not work for me, I was able to do this by setting:

把它放在这里--debug-jvm对我不起作用,我可以通过设置来做到这一点:

 org.gradle.daemon=true
 org.gradle.jvmargs=... -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=10999

in

 ~/.gradle/gradle.properties

But when I connect with eclipse debugger for the project none of the breakpoints I've set compile/trigger... I am connected via the debugger, I can see action in the Debug view, whenever I run gradle testfrom command line, like new threads starting/stopping, but can't get breakpoints to trigger, trying to resolve this now...

但是当我为项目连接 eclipse 调试器时,我没有设置编译/触发的断点...我通过调试器连接,我可以在调试视图中看到操作,每当我从命令行运行时gradle test,就像新线程一样启动/停止,但无法触发断点,现在尝试解决此问题...

Fyi to stop deamon run gradle --stop

仅供参考以停止守护进程 gradle --stop

Other solution

其他解决方案

Leaving above as reference, this worked for triggering break points in tests, I turned off deamon as I could not get it to work properly:

以上留作参考,这适用于在测试中触发断点,我关闭了守护进程,因为我无法让它正常工作:

Using directions from this article: http://blogs.steeplesoft.com/posts/2013/gradle-tip-attaching-a-debugger.html

使用本文中的说明:http: //blogs.steeplesoft.com/posts/2013/gradle-tip-attaching-a-debugger.html

test {        
    if (System.getProperty('DEBUG', 'false') == 'true') {
        jvmArgs '-Xdebug',
            '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=10999'
    }
}

Executed via gradle test -DDEBUG=true

执行通过 gradle test -DDEBUG=true

Solution when using the JUnit Platform Gradle plugin

使用JUnit Platform Gradle插件时的解决方法

The solution above won't work when using org.junit.platform.gradle.plugin.

使用org.junit.platform.gradle.plugin.

Instead it should be replaced by:

相反,它应该替换为:

junitPlatformTest {        
    if (System.getProperty('DEBUG', 'false') == 'true') {
        jvmArgs '-Xdebug',
            '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=10999'
    }
}

回答by janoulle

I'm on 4.6 (gradle) and I'm able to debug my tests when I have this in my build.gradle file:

我在 4.6 (gradle) 上,当我的 build.gradle 文件中有这个时,我可以调试我的测试:

test {
    debug true
}

Link - https://docs.gradle.org/4.6/userguide/userguide_single.html#sec:java_test

链接 - https://docs.gradle.org/4.6/userguide/userguide_single.html#sec:java_test