【问题标题】:Unsure how to launch runner using AbstractTestNGCucumberTests不确定如何使用 AbstractTestNGCucumberTests 启动运行器
【发布时间】:2016-08-17 16:35:23
【问题描述】:

我不确定如何使用 AbstractTestNGCucumberTests 在 Eclipse 中启动运行器类

我建立了一个项目,打算使用 TestNG 来运行 Cucumber 测试套件。

我已按照我能找到的所有文档中的所有步骤进行操作,但我无法做到 以识别 testNG 注释的方式启动运行器 类,如@BeforeMethod

如果我在运行器类中取消注释 @RunWith 行,则项目作为 Junit 测试运行良好,但是当我注释 @RunWith 时,它不会作为 TestNG 项目启动,并且仍然作为 junit 项目运行。

如果我选择 CukesRunner 并单击“运行方式”,则不会显示运行类型,我只能从历史记录中选择以 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 |

这是项目结构的截图:

【问题讨论】:

    标签: testng cucumber-jvm cucumber-java testng-eclipse


    【解决方案1】:

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

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

    @Before(order=1)
    

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

    附:哦,我认为您需要在 @CucumberOptions 部分的 cukes runner 类中添加 glue 选项

    希望对你有帮助。

    【讨论】:

    • 感谢您的回复。我仍然很困惑。
    • 1) 我不明白这个说法:“仍然可以通过手动创建运行配置文件从 cukes runner 执行它(只需在 testNG 运行配置中指定类)”——你的意思是创建在 Eclipse 中运行配置?我根本没有成功,运行配置的任何选项都不起作用.....它们都因各种错误而失败,请参见下面的第 3 项) 2)使用 maven ... 那将是命令行,对?你能提供一个示例命令行吗?
    • 3) 使用黄瓜功能运行器:我已经有一个 cukesrunner 文件,如前所示。如何在 Eclipse 中运行它?如果我在没有 @Runwith 的情况下启动,那么它不再是 Junit 测试。如果我尝试将它作为 Java 应用程序运行,则说明没有主类。如果我尝试将它作为 Java Applet 运行,它会因大量错误而失败。没有用于运行的 TestNG 选项。所以我完全不知道应该如何从 Eclipse 中启动它......我似乎在这里错过了一些非常基本的东西......
    • 4) 您的 Steps 类中的 testNG 注释:是的,它应该是这样工作的,对吧?我还能把注释放在哪里? 5)你说不需要@Test注释......这与我见过的大多数文档相反...... 6)胶水选项:我可以在没有任何胶水选项的情况下运行黄瓜测试,是吗TestNG 需要吗?我仍然无法使用 TestNG 运行 Cucumber 测试.....
    • 1.是的,在 Eclipse 中创建运行配置(展开运行菜单->运行配置->在导航栏中双击 TestNG)。我所做的只是在 Run... 部分中指定了 Class 并提供了必需的 args(通过说 required 我的意思是在我的框架中是必需的,你有可能不需要需要在您的情况下提供任何参数)2. Maven 在我的情况下 maven 命令如下所示:mvn test - 如果直接在项目文件夹或目标中从命令行执行:testpackage selected 如果从 Eclipse 执行(您也可以在 eclipse 中创建 maven 运行配置)
    猜你喜欢
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    • 1970-01-01
    相关资源
    最近更新 更多