gpt4 book ai didi

Java:@AfterStep、@BeforeStep 在 cucumber 中不起作用

转载 作者:行者123 更新时间:2023-11-30 07:43:10 24 4
gpt4 key购买 nike

在我的 Java 和 maven 项目中,我使用 Cucumber 进行 BDD 测试。
这是我使用的依赖项:

    <dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>4.2.0</version>
</dependency>

我的 Hooks 类:

import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.AfterStep;
import cucumber.api.java.BeforeStep;


public class Hooks {

@Before
public void init() {
System.out.println( " Before Scenario " );
}

@After
public void cleanUp() {
System.out.println( " After Scenario " );
}


@BeforeStep
public void beforeStep() {
System.out.println("======> This is before step <======");
//Do something before executing the step
}

@AfterStep
public void afterStep() {
System.out.println("======> This is after step <======");

}
}

但是当我运行 Cucumber 测试时,它只打印 Before ScenarioAfter Scenario与此同时,我希望看到,这是在步骤之前这是在步骤之后之前之后每个步骤。

那么,为什么 @AfterStep@BeforeStep 不起作用?

最佳答案

  • 你应该提供一个小例子,其他人可以用它来重现你的问题

看看这个有效的小片段

假设以下结构

pom.xml
src/test/java/TestRunner.java
src/test/java/stepdefs/StepDefinitions.java
src/test/java/stepdefs/Hooks.java
src/test/resource/features/demo.feature

pom.xml

...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<version.cucumber>4.2.0</version.cucumber>
</properties>

<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${version.cucumber}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${version.cucumber}</version>
<scope>test</scope>
</dependency>
</dependencies>
...

TestRunner.java

import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
features = {"src/test/resource/features"},
glue = {"stepdefs"}
)
public class TestRunner {
}

StepDefinitions.java

package stepdefs;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class StepDefinitions {
@Given("^a successful step$")
public void aSuccessfulStep() {
System.out.println(" aSuccessfulStep()");
}

@When("doing something")
public void doingSomething() {
System.out.println(" doingSomething()");
}

@Then("something happen")
public void somethingHappen() {
System.out.println(" somethingHappen()");
}
}

Hooks.java

package stepdefs;

import cucumber.api.java.After;
import cucumber.api.java.AfterStep;
import cucumber.api.java.Before;
import cucumber.api.java.BeforeStep;

public class Hooks {
@Before
public void init() {
System.out.println("@Before scenario");
}

@After
public void cleanUp() {
System.out.println("@After scenario");
}

@BeforeStep
public void beforeStep() {
System.out.println(" @BeforeStep");
}

@AfterStep
public void afterStep() {
System.out.println(" @AfterStep");
}
}

演示.feature

Feature: Test cucumber reporting plugin

Scenario: Run a non failing scenario
Given a successful step
When doing something
Then something happen

运行测试

$ mvn clean test

产生以下输出

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestRunner
@Before scenario
@BeforeStep
aSuccessfulStep()
@AfterStep
@BeforeStep
doingSomething()
@AfterStep
@BeforeStep
somethingHappen()
@AfterStep
@After scenario

1 Scenarios (1 passed)
3 Steps (3 passed)

关于Java:@AfterStep、@BeforeStep 在 cucumber 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53719701/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com