我不确定如何使用 AbstractTestNGCucumberTests 在 Eclipse 中启动运行器类
我建立了一个项目,打算使用 TestNG 来运行 Cucumber 测试套件。
我已经按照我能找到的所有文档中的所有步骤进行操作,但是我无法以识别类中的 testNG 注释的方式启动运行程序,例如 @BeforeMethod
如果我在 runner 类中取消注释 @RunWith 行,该项目作为 Junit 测试运行良好,但是当我注释 @RunWith 时,它不会作为 TestNG 项目启动,并且仍然作为 junit 项目运行。
如果我选择 CukesRunner 并单击“Run-As”,则不会显示任何运行类型,我只能从历史记录中选择以 Junit 身份运行。我找不到启动 Cukesrunner 的方法,它会调用 AbstractTestNGCucumberTests 类的 TestNG 行为。
TestNG 插件在该系统上运行良好,testNG 启用项目在不包含 cucumber 或 AbstractTestNGCucumberTests 类时运行良好。
以下是该项目的关键组成部分:
testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Cucumber-numbers-game" >
<test name="Play_Games" >
<classes>
<class name="com.example.GameSteps" />
</classes>
</test>
</suite>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>example</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>sonatype-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-testng</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
CukesRunner.java
package com.example;
import cucumber.api.junit.Cucumber;
// import org.junit.runner.RunWith;
import cucumber.api.testng.AbstractTestNGCucumberTests;
// @RunWith(Cucumber.class)
@Cucumber.Options(
features={"src/test/resources"}
)
public class CukesRunner extends AbstractTestNGCucumberTests{}
GameSteps.java
package com.example;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.testng.annotations.*;
import static org.junit.Assert.assertEquals;
public class GameSteps {
private Game _target;
private String _actualResult;
@BeforeMethod
public void setUp() {
System.out.println("@BeforeMethod: The annotated method will be run before each test method.");
}
@Test
@Given("^I am officiating a \"([^\"]*)\" game$")
public void I_am_officiating_a_game(String gameTypeName) {
System.out.println("^I am officiating a \"([^\"]*)\" game$");
GameType gameType = GameType.valueOf(gameTypeName);
_target = GameFactory.createGame(gameType);
}
@When("^the number (\\d+) is played$")
public void the_number_is_played(int playedNumber) {
System.out.println("^the number (\\d+) is played$");
_actualResult = _target.checkPlay(playedNumber);
}
@Then("^I should be told the correct answer is \"([^\"]*)\"$")
public void I_should_be_told_the_correct_answer_is(String expectedResult) {
System.out.println("^I should be told the correct answer is \"([^\"]*)\"$");
assertEquals(expectedResult, _actualResult);
}
}
游戏功能
Feature: Number Games
So that plays can be validated
As a number game umpire
I want to enter a play and see the correct answer
Scenario Outline: A game of FizzBuzz
Given I am officiating a "FizzBuzz" game
When the number <played> is played
Then I should be told the correct answer is "<result>"
Examples:
| played | result |
| 1 | 1 |
| 2 | 2 |
| 3 | Fizz |
| 5 | Buzz |
| 6 | Fizz |
| 10 | Buzz |
| 15 | FizzBuzz |
Scenario Outline: A game of Crash Bang Wallop
Given I am officiating a "CrashBangWallop" game
When the number <played> is played
Then I should be told the correct answer is "<result>"
Examples:
| played | result |
| 1 | 1 |
| 2 | 2 |
| 3 | Crash |
| 5 | Bang |
| 7 | Wallop |
| 15 | CrashBang |
| 35 | BangWallop |
| 21 | CrashWallop |
| 105 | CrashBangWallop |
这是项目结构的截图: