Java:@ AfterStep,@ BeforeStep在黄瓜中不起作用

时间:2018-12-11 08:00:11

标签: java cucumber

在我的Java和Maven项目中,我使用了黄瓜进行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  <======");

    }
}

但是当我运行黄瓜测试时,它只会打印Before ScenarioAfter Scenario 同时 ,我希望看到This is before stepThis is after step 每个步骤

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

1 个答案:

答案 0 :(得分:0)

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

看看这个有效的小片段

假设以下结构

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");
    }
}

demo.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)