serenity-bdd / serenity-cucumber-starter

A skeleton project for Serenity BDD and Cucumber JVM
Apache License 2.0
179 stars 292 forks source link

Cucumber Runner Class for Junit 5 Not finding tests. #116

Open TheGlovner opened 1 year ago

TheGlovner commented 1 year ago

Hopefully someone has already solved this.

I'm trying to get a Serenity Cucumber project running using Junit5.

Issue is two fold, when using mvn clean verify it doesn't seem to find any tests regardless of the following configurations.

If I'm running directly from the TestRunner class configured for Junit4 it can see the tests fine, but when I switch the annotations over to the Juni5 ones then it can't see any of the feature files and instead runs the process and returns "No tests were found".

This test runner will allow me to run directly from the runner and find the feature file:

package swaglabs;

import io.cucumber.junit.CucumberOptions;
import net.serenitybdd.cucumber.CucumberWithSerenity;
import org.junit.runner.RunWith;

@RunWith(CucumberWithSerenity.class)
@CucumberOptions(
        plugin = {"pretty"},
        features = "src/test/resources/features"
)
public class TestRunner {}

But when I switch over to the following I get the "No tests were found" message:

package swaglabs;

import io.cucumber.junit.CucumberOptions;
import net.serenitybdd.junit5.SerenityJUnit5Extension;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(SerenityJUnit5Extension.class)
@CucumberOptions(
        plugin = {"pretty"},
        features = "src/test/resources/features"
)
public class TestRunner {}

Here is the serenity.conf (although I don't think there is anything in here impacting things):

webdriver {
  driver = chrome
  autodownload = true
  base.url = "https://www.saucedemo.com/"
}

headless.mode = false

serenity {
  test.root = starter
  compress.filenames = false
  take.screenshots = BEFORE_AND_AFTER_EACH_STEP
  logging = QUIET
}

#
# Chrome options can be defined using the chrome.switches property
#
chrome.switches = """--start-maximized;--test-type;--no-sandbox;--ignore-certificate-errors;
                   --disable-popup-blocking;--disable-default-apps;--disable-extensions-file-access-check;
                   --incognito;--disable-infobars,--disable-gpu;--remote-allow-origins=*"""

And my current POM.xml setup:

<?xml version="1.0" encoding="UTF-8"?>
<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>net.serenitybdd.starter</groupId>
    <artifactId>serenity-junit-screenplay-starter</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Sample Serenity BDD project using JUnit 5 and Serenity Screenplay</name>

    <properties>
        <serenity.version>3.5.1</serenity.version>
        <junit5.version>5.9.2</junit5.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <encoding>UTF-8</encoding>
        <tags></tags>
        <parallel.tests>4</parallel.tests>
        <webdriver.base.url></webdriver.base.url>
    </properties>
    <dependencies>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-core</artifactId>
            <version>${serenity.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-junit5</artifactId>
            <version>${serenity.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-screenplay</artifactId>
            <version>${serenity.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-ensure</artifactId>
            <version>${serenity.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-screenplay-webdriver</artifactId>
            <version>${serenity.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-cucumber</artifactId>
            <version>${serenity.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.10</version>
        </dependency>
        <!-- JUNIT 5 DEPENDENCY-->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit5.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit5.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>3.22.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.1.2</version>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>3.1.2</version>
                <configuration>
                    <includes>
                        <include>**/*Test.java</include>
                        <include>**/Test*.java</include>
                        <include>**/*TestSuite.java</include>
                        <include>**/When*.java</include>
                    </includes>
                    <systemPropertyVariables>
                        <webdriver.base.url>${webdriver.base.url}</webdriver.base.url>
                        <junit.jupiter.extensions.autodetection.enabled>true
                        </junit.jupiter.extensions.autodetection.enabled>
                    </systemPropertyVariables>
                    <parallel>classes</parallel>
                    <threadCount>${parallel.tests}</threadCount>
                    <forkCount>${parallel.tests}</forkCount>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>16</source>
                    <target>16</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>net.serenity-bdd.maven.plugins</groupId>
                <artifactId>serenity-maven-plugin</artifactId>
                <version>${serenity.version}</version>
                <configuration>
                    <tags>${tags}</tags>
                </configuration>
                <executions>
                    <execution>
                        <id>serenity-reports</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>aggregate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Fingers crossed someone else has this working and can see what I'm clearly missing.

TheGlovner commented 1 year ago

I've tried some further changes with the runner class based on things I've found online, so it's new configuration looks like this, but still the same result of "No tests were found":

package swaglabs;

import net.serenitybdd.junit5.SerenityJUnit5Extension;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.IncludeEngines;
import org.junit.platform.suite.api.SelectClasspathResource;
import org.junit.platform.suite.api.Suite;

import static io.cucumber.core.options.Constants.GLUE_PROPERTY_NAME;
import static io.cucumber.core.options.Constants.PLUGIN_PROPERTY_NAME;

@Suite
@ExtendWith(SerenityJUnit5Extension.class)
@IncludeEngines("cucumber")
@ConfigurationParameter(key=PLUGIN_PROPERTY_NAME , value = "pretty")
@SelectClasspathResource("src/test/resources/features/")
@ConfigurationParameter(key=GLUE_PROPERTY_NAME , value = "swaglabs/stepdefinitions")

public class TestRunner {}
szead commented 1 year ago

It is not easy to figure this out, but I use the dependencies from here: https://serenity-bdd.github.io/docs/guide/maven#cucumber-with-junit-5-dependencies

And can you also try this ConfigurationParameter -> SerenityReporter? @TheGlovner

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

@Suite
@ConfigurationParameter(key = "plugin", value = "io.cucumber.core.plugin.SerenityReporter")
@IncludeEngines("cucumber")
@SelectClasspathResource("/features")
public class CucumberTestSuite {
}

I hope this helps.

wakaleo commented 1 year ago

This is a JUnit 5 configuration issue of some kind.

akalita-mms commented 3 months ago

@SelectClasspathResource does not work in Linux for junit platform engine. Workaround is to use FEATURES_PROPERTY_NAME

VivekJoshi99 commented 2 months ago

I tried the snippet below, It picks all the scenarios defined on the feature file irrespective of whether they have a @smoke tag. I am using the following versions: Serenity - 4.1.12 Cucumber - 7.13.0 JUnit - 5.10.0 maven-surefire - 3.0.0-M5 maven-failsafe - 3.3.0 maven-compiler - 3.8.1

package Runner;

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

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

@Suite @IncludeEngines("cucumber") @ConfigurationParameter(key = FEATURES_PROPERTY_NAME, value = "classpath:Features") //@SelectClasspathResource("Features") // Adjust this to your actual feature file location @ConfigurationParameter(key = FILTER_TAGS_PROPERTY_NAME, value = "@smoke") @ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "StepDefinitions") // Adjust to your step definitions package @ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty,html:target/cucumber-html-report,summary") public class TestRunnerSerenity { }