我试图在vscode中调试黄瓜步骤定义,但没有运气。
该项目相应配置了官方手册Cucumber Java Tools。它编译精细并使用命令显示黄瓜输出:
gradle cucumber
为了附加到守护进程,以下几行代码被添加到gradle.properties
:
org.gradle.daemon=true
org.gradle.jvmargs=-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005
似乎vscode附加正常,因为我可以看到调用堆栈在vscode中弹出。甚至可以打破“被捕的例外”。但根本不会触发“自定义”断点。
launch.json
中使用以下调试配置:
"type": "java",
"name": "Debug (Attach)",
"request": "attach",
"hostName": "localhost",
"port": 5005
这是gradle.build
:
plugins {
id 'java'
}
repositories {
flatDir {
dirs 'libs'
}
jcenter()
mavenCentral()
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.google.guava:guava:27.1-jre'
compile group: 'org.testng', name: 'testng', version: '6.14.3'
testImplementation 'io.cucumber:cucumber-java:4.2.6'
}
configurations {
cucumberRuntime {
extendsFrom testImplementation
}
}
task cucumber() {
dependsOn assemble, compileTestJava
doLast {
javaexec {
main = "cucumber.api.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = ['--plugin', 'pretty', '--glue', 'gradle.cucumber', 'src/test/resources']
}
}
}
sourceCompatibility = '11'
targetCompatibility = '11'
version = '1.2.1'
笔记:
奇怪的是,使用默认的黄瓜的java runner不允许Visual Studio Code和Eclipse远程调试器在步骤定义上设置断点。
但是可以通过使用cucumber的junit4 runner来解决这个问题。这是一个更新的gradle配置(注意,你不再需要“黄瓜”任务了):
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.google.guava:guava:27.1-jre'
// used for running cucumber steps + powermock
testCompile 'junit:junit:4.12'
testCompile 'io.cucumber:cucumber-java:4.3.0'
testCompile 'io.cucumber:cucumber-junit:4.3.0'
}
请注意,junit:junit
依赖项还包含一个junit runner。然后你可以创建一个空类,例如:JUnitRunnerWrapper
,它将包含黄瓜的配置(通过注释):
@RunWith(Cucumber.class)
@CucumberOptions(
plugin = { "pretty", "html:build/reports/tests/cucumber-html-report" },
glue = { "gradle.cucumber" },
features = "src/test/resources",
monochrome = true)
public class JUnitRunnerWrapper {
}
为了使它工作,你必须为vscode安装Java Test Runner
。然后你就可以在JUnitRunnerWrapper
下看到“运行测试/调试测试”:
按“Debug Test”后,vscode将启动测试并触发断点:
补充说明:
gradle test
命令运行gradle任务Run Test
命令显示Java: Show Test Output
命令的输出