1

我不确定如何使用 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 |

这是项目结构的截图:

这是项目结构的截图:

4

1 回答 1

0

是的,从你的 cukes 运行器运行测试时没有 testNG 运行类型,但是仍然可以通过手动创建运行配置文件从 cukes 运行器执行它(只需在你的 testNG 运行配置中指定类),但我相信它不是最好的方法,所以如果你想在本地执行你的功能,我会提出 2 个选项:

  • cucumber.options如果要执行单个功能,请使用 Maven 并指定参数
  • 使用黄瓜功能运行器(要安装它,请在 Eclipse 中添加以下存储库: https ://cucumber.io/cucumber-eclipse/update-site )

我看到由于某种原因你试图在你的 Steps 类中使用 testNG 注释,排除@Test@BeforeMethod注释,如果你需要设置你的测试考虑使用@Before来自 Cucumber API 的注释,你实际上甚至可以@Before's像这样指定你的顺序:

@Before(order=1)

而且根本不需要@Test注释,您在功能文件中指定您的测试。

PS哦,我认为你需要glue在你的cukes runner类中@CucumberOptions添加选项

希望能帮助到你。

于 2016-08-18T19:36:46.683 回答