简体   繁体   中英

TestRunner in Cucumber does not run my feature files

I have been trying to run my feature files using TestRunner but it has failed to do so. When I right click on TestRunner -> Run As JUnit, nothing happens and it just shows Finished. Console tab has no messages and JUnit tab has no test runs. It just shows "finished after X seconds".

I attached my package structure for you to check.

I am using Gradle and Eclipse IDE

Here are is my TestRunner Class:

package runners;

import org.junit.runner.RunWith;
import io.cucumber.junit.*;
    
@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/test/java/feature",
        glue = {"stepDefinition"},
        tags = "@Login",
    )

public class TestRunner {
    
}

here is my code for step definition:

package stepDefinition;

import org.openqa.selenium.WebDriver;

import cucumber.TestContext;
import io.cucumber.java.en.*;
import managers.FileReaderManager;
import dataProvider.ConfigFileReader;

public class CommonStep {
    
    private WebDriver driver;
    private TestContext testContext;
    
    public CommonStep (ConfigFileReader configFile, TestContext testContext) {
        
        this.testContext = testContext;
        this.driver = testContext.getWebDriverManager().getDriver();
    }
    
    @Given("the user navigates to Login Page {string}")
    public void TheUserIsInLoginPage(String url) throws Throwable {
        
        driver.get(FileReaderManager.getInstance().getConfigReader().getApplicationUrl());
        testContext.getPageObjectManager().getBasePage().waitForPageToLoad(30);;
        
        Thread.sleep(5000);
    }
}

Am I missing something? Thank you for your help!

I tried putting my feature files under src/test/resources folder and changed my TestRunner features = "src/test/resources/feature" and still no luck. I am getting the same output as the original issue.

I believe your problem is the JUnit version your Test Runner class is using. Try executing your TestRunner.java with JUnit 4. To do this, open the Run Configurations context menu for your test runner class, and select JUnit 4 from the drop down.

在此处输入图像描述

The fact you are using @CucumberOptions tells me you are using a JUnit 4 Cucumber configuration. This is why when you try to run your test as a JUnit test it fails, but when you run it as a Cucumber Feature it works fine. Basically, you are taking JUnit out of the picture.

If you have JUnit 5 (Jupiter) installed, it tends to default to this version to run your tests. Cucumber with JUnit 4 and 5 configurations are not really compatible. If you want to use JUnit 5, I suggest you configure your project following these instructions . I must clarify that the linked example runs tests as Maven tests and not as JUnit test, but does use the JUnit 5 platform.

EDIT :

Just to give you a quick rundown on how to convert your project to Cucumber using JUnit 5.

Your runner, step definitions, and feature files must under a common package. For my sample project, I created the com.example package. Therefore, your structure should be something like this:

src/test/java
  |
  - com/example/runner/TestRunner.java
  - com/example/definitions/StepDefinitions.java

src/test/resources
  |
  - com/example/features/MyTests.feature

Your TestRunner class should be annotated as follows:

import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME;

import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.SelectClasspathResource;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectClasspathResource("com/example")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.example")
public class Runner {}

And, as a minimum, your POM should contain the following

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>SeleniumWithCucumber</groupId>
    <artifactId>SeleniumWithCucumber</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SeleniumWithCucumber</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <cucumber.version>7.10.1</cucumber.version>
        <selenium.version>4.4.0</selenium.version>
        <webdrivermanager.version>5.2.1</webdrivermanager.version>
        <junit.jupiter.version>5.9.1</junit.jupiter.version>
        <apache.common.version>2.4</apache.common.version>
        <projectlombok.version>1.18.24</projectlombok.version>
        <maven.compiler.plugin.version>3.10.1</maven.compiler.plugin.version>
        <maven.surefire.plugin.version>3.0.0-M7</maven.surefire.plugin.version>
        <maven.compiler.source.version>17</maven.compiler.source.version>
        <maven.compiler.target.version>17</maven.compiler.target.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.cucumber</groupId>
                <artifactId>cucumber-bom</artifactId>
                <version>${cucumber.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.junit</groupId>
                <artifactId>junit-bom</artifactId>
                <version>${junit.jupiter.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit-platform-engine</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- JUnit Platform -->
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Selenium -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>${selenium.version}</version>
        </dependency>
        <!-- Web Driver Manager -->
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>${webdrivermanager.version}</version>
        </dependency>
        <!-- Apache Common -->
        <dependency>
            <groupId>org.apache.directory.studio</groupId>
            <artifactId>org.apache.commons.io</artifactId>
            <version>${apache.common.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${projectlombok.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>${maven.compiler.source.version}</source>
                    <target>${maven.compiler.target.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven.surefire.plugin.version}</version>
                <configuration>
                    <properties>
                        <configurationParameters>
                cucumber.junit-platform.naming-strategy=long
            </configurationParameters>
                    </properties>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Lastly, make sure your test runner class uses JUnit 5 instead of JUnit 4. I already provided instructions on how to change this parameter in your run configurations context menu.

In short, you have two options :

  1. Change your current project configuration to run with JUnit 4, or
  2. Change your project as I indicated in this last edit so it can run with JUnit 5.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM