且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在vscode中调试gradle+cucumber时不触发断点

更新时间:2022-12-19 07:41:41

Strangely, using the default cucumber's java runner doesn't allow Visual Studio Code nor Eclipse remote debugger to set a breakpoint on step definition.

But it's possible to solve this issue by using cucumber's junit4 runner. Here is an updated gradle configuration (notice, you don't need the "cucumber" task anymore):

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'
}

Note that junit:junit dependency also contains a junit runner. Then you can create an empty class, e.g.: JUnitRunnerWrapper that will contain cucumber's configuration (via annotations):

@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 { 
}

In order to make it work you have to install Java Test Runner for vscode. Then you'll be able to see the "Run Test/Debug Test" under the JUnitRunnerWrapper:

After pressing "Debug Test", vscode will launch the tests and breakpoints will be triggered:

Additional notes:

  1. You can still run the gradle task via gradle test command
  2. The output of the Run Test command can be shown using vscode Java: Show Test Output command