@Before AbstractTestNGCucumberTests应该按照方案java执行

时间:2018-01-19 00:47:51

标签: testng cucumber-jvm

我正在使用Cucumber和Test Ng进行行为驱动开发的集成测试。我在行为跑步者以下

@CucumberOptions(plugin = { "json:test-output/cucumber-
                           report/report1.json", "html:test-
                           output/cucumber-report/report1.html" }, 
                glue = "com.package.behavior", 
                features = "src/test/resources/behavior/feature1/")
public class BehaviorRunner1 extends AbstractTestNGCucumberTests {
    @Before
    public void beforeScenario(Scenario scenario) {
        log.error("Executing scenario "+scenario.getName());
    }
}

@CucumberOptions(plugin = { "json:test-output/cucumber-
                             report/report2.json", "html:test-
                             output/cucumber-report/report2.html" }, 
                 glue = "com.package.behavior", 
                 features = "src/test/resources/behavior/feature2/")
public class BehaviorRunner2 extends AbstractTestNGCucumberTests {
    @Before
    public void beforeScenario(Scenario scenario) {
        log.error("Executing scenario "+scenario.getName());
    }
}

文件夹结构为: -

src
 |
 |-test
    |
    |-resources
        |
        |-behavior
            |
            |-feature1
            |    |
            |    |-feature-file1.feature
            |-feature2
            |    |
            |    |-feature-file2.feature

feature-file1.feature
------------------------------------
Scenario : Test Scenario1

feature-file2.feature
------------------------------------
Scenario : Test Scenario2

当我执行方案时,我在下面看到打印两次。

    执行场景测试场景1     执行场景测试场景1     执行场景测试场景2     执行场景测试场景2

我想看下面的输出

    执行场景测试场景1     执行场景测试场景2

pom.xml

<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>1.2.5</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-testng</artifactId>
    <version>1.2.5</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-spring</artifactId>
    <version>1.2.5</version>
    <scope>test</scope>
</dependency>

<dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-java</artifactId> <version>1.2.5</version> <scope>test</scope> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-testng</artifactId> <version>1.2.5</version> <scope>test</scope> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-spring</artifactId> <version>1.2.5</version> <scope>test</scope> </dependency>

我只希望Behavior runner 1从文件夹feature1执行方案,Behavior runner 2从文件夹feature2执行方案。

1 个答案:

答案 0 :(得分:0)

您将获得两个日志条目,因为有两个@Before挂钩。钩子本质上是全局的,即在胶合选项包结构中找到的所有钩子,并且在这种情况下将针对每个场景执行两次。挂钩之前只保留一个。或者,如果您需要多个钩子,请使用注释的顺序值来创建链。您还可以在注释之前使用标记来过滤挂钩之前的标记。

您可能希望将挂钩移动到glue选项包中的单独类。更容易找到钩子。同样使用当前设置,您必须在glue选项中包含测试运行器类,从而赋予它们两个职责。