【问题标题】:Using JUnit Hooks with Cucumber CLI Runner将 JUnit Hooks 与 Cucumber CLI Runner 一起使用
【发布时间】:2022-01-13 21:32:03
【问题描述】:

我正在尝试使用 Cucumber 的 CLI Runner 并行运行 Cucumber 的功能文件,我目前正试图弄清楚如何使 JUnit @BeforeClass 挂钩与 CLI Runner 一起工作。

目前,我的 工作 Runner 类如下所示:

@RunWith(Cucumber.class)
@CucumberOptions(
    plugin = {
      "pretty",
      "html:target/reports/basic/report.html",
      "json:target/reports/cluecumber/cucumber.json",
      "timeline:target/reports/timeline"
    },
    tags = "@RegressionTests",
    snippets = SnippetType.CAMELCASE,
    stepNotifications = true,
    features = "classpath:features",
    glue = "my.steps.package")
public class RegressionTestsIT {

  @BeforeClass
  public static void setup() {
    ContextHolder.setupTestContext();
  }
}

我的 CLI 命令如下所示:

java -cp "target/test-jar-with-dependencies.jar" io.cucumber.core.cli.Main -p "pretty" -p "html:target/reports/basic/report.html" -p "json:target/reports/cluecumber/cucumber.json" -p "timeline:target/reports/timeline" --threads 10 -g "my.steps.package" target/test-classes/features

发生的情况是我在测试中收到 NullPointerException,因为未正确设置 TestContext,因为未执行钩子。

我尝试将 Runner 的包和 Runner 类本身作为胶水包含在内,但它不起作用。

还尝试让我的 Runner 扩展 io.cucumber.core.cli.Main 然后在 CLI 中执行我的 Runner,毫不奇怪它也没有工作,遗憾的是仍然得到 NPE。

虽然这个问题与 CLI Runner 的使用有关,但我对任何可能帮助我并行运行多个功能文件的答案感到满意。

【问题讨论】:

  • 你能把--threads 10改成-threads 10试试看。请查看它可能会有所帮助ghchirp.tech/283
  • 您确定在 test-jar-with-dependencies.jar 中有 JUnit 吗?
  • 谢谢大家,找到适合我的解决方案。
  • @AlexeyR,是的,JUnit 被捆绑在了 jar 中。
  • (也许 Cucumber CLI Runner 根本不使用 cucumber-junit?)

标签: java junit cucumber integration-testing cucumber-java


【解决方案1】:

JUnit @BeforeClass 对我不起作用。由于我对此有点着急,所以我没有费心继续尝试使它起作用。目前我真的不需要在管道中运行命令,所以只要并行运行,我就可以在 IntelliJ 上运行它。

我的解决方案是创建一个自定义 CLI Runner,它在 Cucumber 的 CLI 运行方法之前运行上下文配置。

public class CLIRunner {

  public static void main(String[] args) {
    ContextHolder.setupTestContext();

    io.cucumber.core.cli.Main.run(
        new String[] {
            "-p", "pretty",
            "-p", "html:target/reports/basic/report.html",
            "-p", "json:target/reports/cluecumber/cucumber.json",
            "-p", "timeline:target/reports/timeline",
            "-g", "my.steps.package",
            "classpath:features",
            "--threads", "10"
        }, Thread.currentThread().getContextClassLoader());
  }
}

【讨论】:

    【解决方案2】:

    Using JUnit Rules

    Cucumber 支持 JUnit 的 @ClassRule、@BeforeClass 和 @AfterClass 注释。这些将在所有场景之前和之后执行。不推荐使用这些,因为它限制了不同跑步者之间的可移植性;使用命令行、IntelliJ IDEA 或 Cucumber-Eclipse 时,它​​们可能无法正确执行。而是建议use Cucumber's hooks

    使用 CLI 时,根本不涉及 JUnit,因此您不能使用任何 JUnit 注释。但是,从 Cucumber v7 开始,您可以使用 @BeforeAll@AfterAll 声明在所有场景之前和之后执行的方法。

    package io.cucumber.example;
    
    import io.cucumber.java.AfterAll;
    import io.cucumber.java.BeforeAll;
    
    public class StepDefinitions {
    
        @BeforeAll
        public static void beforeAll() {
            // Runs before all scenarios
        }
    
        @AfterAll
        public static void afterAll() {
            // Runs after all scenarios
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 2013-07-10
      相关资源
      最近更新 更多