1

I am Unsure how to launch the runner class within Eclipse using AbstractTestNGCucumberTests

I have a project set up, intended to use TestNG to run a Cucumber test suite.

I have followed all the steps in all the docs I can find, but I am unable to launch the runner in a manner which recognizes the testNG annotations in the class, such as @BeforeMethod

The project runs fine as a Junit test if I uncomment the @RunWith line in the runner class, but when I comment the @RunWith, then it does not launch as a TestNG project, and still behaves as a junit project.

If I select the CukesRunner an click "Run-As" there is no run type displayed, and I can only select to run it as Junit from the history record. I can find no way of launching the Cukesrunner such that it invokes the TestNG behavior of the AbstractTestNGCucumberTests class.

The TestNG plugin is working fine on this system, testNG enable projects run fine when they do not include the cucumber nor AbstractTestNGCucumberTests class.

Here are the key components of the project:

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

Game.feature

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 |

Here is a screenshot of the project structure:

Here is a screenshot of the project structure:

warunapww
  • 966
  • 4
  • 18
  • 38
Charles F Radley
  • 163
  • 1
  • 2
  • 11

1 Answers1

0

Yes, there is no testNG run type while running tests from your cukes runner, however it is still possible to execute it from cukes runner by creating running profile manually(just specify class in your testNG run config), however I believe that it's not the best way to do it, so if you want to execute your features locally I would propose 2 options:

I see that for some reason you're trying to use testNG annotations in your Steps class, exclude @Test and @BeforeMethod annotations, if you need to set up your test consider using @Before annotation from Cucumber API, you actually can even specify order of your @Before's like this:

@Before(order=1)

And there is no need for @Test annotations at all, you're specifying your test in your feature file.

P.S. Oh, and I think that you need to add glue option in your cukes runner class in @CucumberOptions section

Hope it helps.

Mikhail
  • 665
  • 4
  • 16
  • Thanks for your reply. I am still a but puzzled. – Charles F Radley Aug 19 '16 at 23:43
  • 1) I do not understand this statement: " it is still possible to execute it from cukes runner by creating running profile manually(just specify class in your testNG run config)" -- do you mean creating run configurations in Eclipse ? I have had no success at all with that, none of the options for run configuration work ..... they all fail with various errors, see item-3) below 2) Using maven ... that would be command line, right ? Can you provide a sample command line ? – Charles F Radley Aug 19 '16 at 23:53
  • 3) Using cucumber feature runner : I already have a cukesrunner file as shown previously. How do I run it within Eclipse ? If I launch without the @Runwith then it ceases to be a Junit test. If I try to run it as a Java Application is fails saying there is no main class. If I try to run it as a Java Applet it fails with a bucnh of errors. There is no TestNG option for running. so I am at a complete loss as to how one is supposed to launch this from within eclipse .... I seem to be missing something really basic here ... – Charles F Radley Aug 19 '16 at 23:53
  • 4) testNG annotations in your Steps class: yes, that is how it is supposed to work, right ? Where else would i put the annotations ? 5) You say there is no need for an @Test annotation ... that is contrary to most of the docs I have seen .... 6) Glue option: I am able to run cucumber tests without any glue option, is it needed for TestNG ? I am still unable to run the Cucumber test with TestNG ..... – Charles F Radley Aug 19 '16 at 23:53
  • 1. Yes, create run configuration in Eclipse(Expand Run menu->Run configurations->Double click TestNG in the nav bar). All I did is specified **Class** in **Run...** section and provided required args (by saying required I mean required in my framework, there is a chance that you don't need to provide any args in your case) 2. Maven in my case maven command looks like this: **mvn test** - if executed from command line directly in project's folder OR goal: **test** and _package selected_ if being executed from Eclipse (You're able to create maven run configs in eclipse as well) – Mikhail Aug 22 '16 at 15:33
  • 3. In order to use feature runner you need to execute feature directly not from your cukes runner, i.e. open feature file and Run->Run as...->Cucumber feature. If you need to provide any args, you're able to do it in **Arguments** tab 4. You don't need to put any **@Test** annotations anywhere when writing tests using features, that is basically why you're extending **AbstractTestNGCucumberTests** P.S. One important thing: When you're running features directly as described in **3** your runner is NOT applied, i.e. all options specified in runner will not be applied. – Mikhail Aug 22 '16 at 15:37
  • Short summary on runners: When running using your cukes runner **OR** using maven - runner affects your test execution (you can specify glue, features to run in it and other options). You can override runner params using **cucumber.options** using VM arguments tab, e.g.: -Dcucumber.options="--format json-pretty". When running single feature using **Cucumber feature runner** from feature file directly - runner does not affect your execution. – Mikhail Aug 22 '16 at 15:47