在vscode中调试gradle+cucumber时不触发断点 [英] Breakpoints are not triggered while debugging gradle + cucumber in vscode

查看:109
本文介绍了在vscode中调试gradle+cucumber时不触发断点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 vscode 中调试黄瓜步骤定义,但没有运气.

使用官方手册

按下调试测试"后,vscode 将启动测试并触发断点:

附加说明:

  1. 你仍然可以通过gradle test命令运行gradle任务
  2. Run Test 命令的输出可以使用 vscode Java: Show Test Output 命令显示

I was trying to debug cucumber step definition in vscode but without luck.

The project was configured accordingly with an official manual Cucumber Java Tools. It compiles fine and shows cucumber output using command:

gradle cucumber

In order to attach to the daemon the following lines of code were added to gradle.properties:

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

It seems like vscode is attaching fine because I can see a call stack is popping up and down in vscode. It's even possible to break on "Caught Exceptions". But "custom" breakpoints are not triggered at all.

The following debug configuration is used in launch.json:

"type": "java",
"name": "Debug (Attach)",
"request": "attach",
"hostName": "localhost",
"port": 5005

Here is 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'

Notes:

  1. I've tried to attach to running gradle daemon using eclipse but it seems like it doesn't work too.

解决方案

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

这篇关于在vscode中调试gradle+cucumber时不触发断点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆