DEV Community

Terence Pan
Terence Pan

Posted on • Edited 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 (4)

Collapse
 
hendisantika profile image
Hendi Santika

Nice article & tutorial.

I want to ask you something.
What is the report tools for Playwright (allure, cucumber report)?

Thanks

Collapse
 
terencepan profile image
Terence Pan • Edited

You can publish reports to the cucumber service which is a cloud service with public urls, generate Json/html/other reports with built in plugins on your local or use community maintained plugins.

Allure is an option too but I haven't used it yet.

The instructions to use the cucumber service are in the output by default when you run cucumber.

At work I regularly use the local reporting function with html reports. I can put up a tutorial for html reports when I find some time. You can also embed screenshots which is useful especially for test failures.

Collapse
 
hendisantika profile image
Hendi Santika

OK.

Can't wait for that.

Thanks.

Thread Thread
 
terencepan profile image
Terence Pan

Thanks, posted a sample html report at: dev.to/terencepan/set-up-cucumber-...