企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 一句话引发的"血案" 官方文档中关于test的信息有这么一句话: ~~~ The test process can be started in debug mode (see getDebug()) in an ad-hoc manner by supplying the `--debug-jvm` switch when invoking the build. gradle someTestTask --debug-jvm ~~~ 但是如果我们在命令行下敲这个命令,会出现如下信息: ~~~ D:\eclipse\workspare\TestNG_gradl>gradle clean test --debug-jvm :clean :compileJava :processResources :classes :compileTestJava :processTestResources :testClasses :test Listening for transport dt_socket at address: 5005 > Building 87% > :test ~~~ 光标会一直在这闪烁,到底有啥用呢?搞了一上午,找到了[一篇文章](http://www.cnblogs.com/warrior/p/3835240.html),恍然大悟。 这个--debug-jvm的参数会在执行测试的时候一直监听5005端口的信息,那么这个信息从哪里来?我慢慢道来。 ## 2.以TestNG为例 我的测试任务代码如下,就是启动TestNG测试,然后在测试前输出一句话,测试case中的输出信息也要输出。 ~~~ test { useTestNG() //systemProperty 'some.prop', 'value' //exclude 'org/gradle/SimpleTest.class' //reports.html.enabled = true //scanForTestClasses = false beforeTest { descriptor -> logger.lifecycle("Running test: " + descriptor) } onOutput { descriptor, event -> logger.lifecycle("Test: " + descriptor + " produced standard out/err: " + event.message ) } } ~~~ 这个时候我们在eclipse中debug模式启动我们的case。但是要注意一点不是直接Debug as->TestNG。具体配置如下: ![](https://box.kancloud.cn/2016-01-07_568e466c0ddb7.jpg) 进入Debug Configurations界面 ![](https://box.kancloud.cn/2016-01-07_568e466c36b94.jpg) 点New以后会自动添加你要测试的类,但是这个时候要注意一定要把端口改成你在命令行监听的端口号5005。如果命令行上是其他端口,只要跟其一样就行。 ![](https://box.kancloud.cn/2016-01-07_568e466c52271.jpg) 这个时候你点击Debug就会发现,命令行有输出了。 ~~~ Running test: test method aFastTest(org.gradle.SimpleTest) Test: test method aFastTest(org.gradle.SimpleTest) produced standard out/err: fa st test Running test: test method aSlowTest(org.gradle.SimpleTest) Test: test method aSlowTest(org.gradle.SimpleTest) produced standard out/err: sl ow test Test: test 'Gradle test' produced standard out/err: tearDown Test: test 'Gradle test' produced standard out/err: setUp Running test: test method aFastTest1(org.gradle.SimpleTest1) Test: test method aFastTest1(org.gradle.SimpleTest1) produced standard out/err: fast test1 Running test: test method aSlowTest1(org.gradle.SimpleTest1) > Building 87% > :test > 3 tests completed ~~~ 而且还会等待debug的case结束。这就是debug模式.