Search code examples
javamavenselenium-webdrivertestngjunit5

Configuring Maven to run TestNG and JUnit5 tests in one project


I need to be able to run TestNG and JUnit5 test classes in my project separately and run them together as well using testng.xml file. Previously I used JUnit4 but needed to swap to the newer JUnit version to use @ParameterizedTest with @CsvSourse. And now, the JUnit test class executed successfully, but when I ran TestNG test class - it tried to be executed using org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.

To run test class I used this command:

mvn clean test -Dtest='src/test/java/tests/FiltersTest'

How I can fix the problem and make both runners work as expected?

P.S. I know that it's not the best approach to use two different runners inside the same project, but I need to do this according to the task from the Automation course I am taking part in.

POM.xml:

<?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>org.example</groupId>
    <artifactId>report-portal</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.14.0</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.30</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.8.0</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-testng</artifactId>
            <version>2.24.0</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>5.5.3</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.6</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.32</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.14.0</version>
        </dependency>

        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>2.2</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.10.0</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.10.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.typesafe</groupId>
            <artifactId>config</artifactId>
            <version>1.4.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.1.2</version>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>

                    <suiteXmlFiles>
                        <suiteXmlFile>src/test/resources/testng-report_portal.xml</suiteXmlFile>
                    </suiteXmlFiles>

                    <systemPropertyVariables>
                        <browser>chrome</browser>
                    </systemPropertyVariables>

                    <systemProperties>
                        <property>
                            <name>allure.results.directory</name>
                            <value>target/allure-results</value>
                        </property>
                        <property>
                            <name>allure.report.directory</name>
                            <value>target/allure-report</value>
                        </property>
                    </systemProperties>
                </configuration>
            </plugin>
            <plugin>
                <groupId>io.qameta.allure</groupId>
                <artifactId>allure-maven</artifactId>
                <version>2.10.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
            </plugin>
        </plugins>
    </build>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <allure.testng.version>2.22.2</allure.testng.version>
    </properties>

</project>

testng.xml suite:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Report Portal Tests" verbose="1">
    <test name="ReportPortalTest">
        <classes>
            <class name="tests.FiltersTest"/>
            <class name="tests.LaunchViewTest"/>
        </classes>
    </test>
    <test name="JunitTest">
        <classes>
            <class name="tests.JunitTest"/>
        </classes>
    </test>
    <listeners>
        <listener class-name="io.qameta.allure.testng.AllureTestNg"/>
    </listeners>
</suite>

TestNG test class example:

package tests;

import core.TestDataProvider;
import jdk.jfr.Description;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;
import java.util.List;
import static org.testng.AssertJUnit.*;

public class FiltersTest extends CommonConditions {

    @Test(priority = 1)
    @Description("Login testing")
    public void loginTest() {
        loginAction.doLogin();
        assertTrue(loginAction.isLoginSuccessful());
    }

    @Test(priority = 2, dataProvider = "Launch name + Launch Number", dataProviderClass = TestDataProvider.class)
    @Description("Add filter: Launch name + Launch number")
    public void setLaunchNameAndLaunchNumber(String launchName, String filterInput, int expectedResult) throws InterruptedException {
        loginAction.doLogin();
        sidebarAction.openLaunchesPage();
        filtersAction.activateFiltering();
        filtersAction.addLaunchNameFilter(launchName);
        filtersAction.addFilterFromDrpDwn("Launch Number", filterInput);
        filtersAction.switchLaunchNumberToEqual();
        Thread.sleep(3000);
        List<WebElement> objectList = driver.findElements(By.xpath("//td[2]//a//span[contains(., '"+launchName+"')]"));
        assertEquals(expectedResult, objectList.size());
    }

    @Test(priority = 3, dataProvider = "Launch name + Passed", dataProviderClass = TestDataProvider.class)
    @Description("Add filter: Launch name + Passed")
    public void setLaunchNameAndPassed(String launchName, String filterInput, int expectedResult) throws InterruptedException {
        loginAction.doLogin();
        sidebarAction.openLaunchesPage();
        filtersAction.activateFiltering();
        filtersAction.addLaunchNameFilter(launchName);
        filtersAction.addFilterFromDrpDwn("Passed", filterInput);
        Thread.sleep(3000);
        List<WebElement> objectList = driver.findElements(By.xpath("//td[2]//a//span[contains(., '"+launchName+"')]"));
        assertEquals(expectedResult, objectList.size());
    }
}

Please let me know if you need additional info.


Solution

  • You can try configurations like:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.2.1</version>
        <configurations>
        <!-- ... your configurations ... -->
        </configurations> 
        <dependencies>
          <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit-platform</artifactId>
            <version>3.2.1</version>
          </dependency>
          <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-testng</artifactId>
            <version>3.2.1</version>
          </dependency>
        </dependencies>
      </plugin>
    

    Which enable Junit 5 and TestNg executors / providers in one build.

    Source: https://maven.apache.org/surefire/maven-surefire-plugin/examples/providers.html