DEV Community 👩‍💻👨‍💻

Terence Pan
Terence Pan

Posted on • Updated on

Playwright with Cucumber/JUnit 5 - JUnit 5 test suite and maven clean install

JUnit 5 Test Suite

In previous JUnit versions there was the TestRunner classes to run tests.

In JUnit5, this is now done through Test Suites. If you followed the other posts in the series and now tried to run the maven command: mvn clean install and find out tests aren't run at all, this might be why. Maven install with the Surefire plug in will look for test classes matching the patterns:
-"/Test*.java" - includes all of its subdirectories and all Java filenames that start with "Test".
-"
/Test.java" - includes all of its subdirectories and all Java filenames that end with "Test".
-"
/*Tests.java" - includes all of its subdirectories and all Java filenames that end with "Tests".
-"
*/*TestCase.java" - includes all of its subdirectories and all Java filenames that end with "TestCase".

To include support for JUnit5 Test suites, pom.xml would need the junit-platform-suite dependency:

        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite</artifactId>
            <scope>test</scope>
        </dependency>
Enter fullscreen mode Exit fullscreen mode

If you need to support other class names that have to be matched aside from the above, just include them in the Surefire configuration in your pom.xml such as below:

        <configuration>
          <includes>
            <include>example.java</include>
            <include>**/*IT.java</include>
            <include>**/*EndToEnd.java</include>
          </includes>
        </configuration>
Enter fullscreen mode Exit fullscreen mode

In my example I will be creating a test suite named DemoRequestTests. All we have to do is include annotations here:

  • @suite: JUnit5 test suite class
  • @SuiteDisplayName("Test Use Cases"): name of the test suite is Test Use Cases
  • @IncludeEngines("cucumber"): run this test suite using Cucumber
  • @SelectClasspathResource("Features"): where the feature files are located, in this case under test/resources/Features
  • @ConfigurationParameter: not used but if your steps are located in a directory that's not automatically picked up by the cucumber engine, then you will want to set the package path to your stepdefinition glue classes.

Now you can run mvn clean install and any test suites you created will run automatically

Code for DemoRequestTests.java

package io.tpan.suites;

import org.junit.platform.suite.api.*;

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

@Suite
@SuiteDisplayName("Test Use Cases")
@IncludeEngines("cucumber")
@SelectClasspathResource("Features")
//@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "steps")
public class DemoRequestTests {
}
Enter fullscreen mode Exit fullscreen mode

As always code is on available on Github

Top comments (0)

Want to rep DEV and be comfy at the same time?

Check out our classic DEV shirt — available in multiple colors.