• Javascript
  • Python
  • Go

Running Single Test Class or Group with Surefire and TestNG

Running tests is an essential part of software development. It allows developers to ensure that their code is functioning as intended and ca...

Running tests is an essential part of software development. It allows developers to ensure that their code is functioning as intended and catches any bugs or errors that may arise. In order to run tests efficiently, there are various tools and frameworks available, such as Surefire and TestNG. In this article, we will explore how to run a single test class or a group of test classes using these two tools.

First, let's understand what Surefire and TestNG are. Surefire is a plugin for Apache Maven which is a build automation tool primarily used for Java projects. It is responsible for executing tests and generating reports. TestNG, on the other hand, is a testing framework that supports various types of automated tests, such as unit, functional, and integration tests. It provides features like test grouping, parallel execution, and data-driven testing.

Now, let's dive into the steps for running a single test class or group of test classes using Surefire and TestNG.

1. Setting up Surefire and TestNG

The first step is to set up Surefire and TestNG in your project. This can be done by adding the necessary dependencies to your project's pom.xml file if you are using Maven. For TestNG, you will need to add the following dependency:

<dependency>

<groupId>org.testng</groupId>

<artifactId>testng</artifactId>

<version>7.3.0</version>

</dependency>

For Surefire, you will need to add the following plugin:

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-surefire-plugin</artifactId>

<version>3.0.0-M5</version>

</plugin>

2. Creating a Test Class

Next, you will need to create a test class that contains the tests you want to run. Let's say we have a class named "Calculator" with the following methods:

- add(int a, int b)

- subtract(int a, int b)

- multiply(int a, int b)

- divide(int a, int b)

We can create a test class named "CalculatorTest" with test methods for each of these methods in the Calculator class.

3. Running a Single Test Class

To run a single test class using Surefire and TestNG, you will need to add the following configuration to your pom.xml file:

<configuration>

<includes>

<include>CalculatorTest.java</include>

</includes>

</configuration>

This will specify which test class to run when executing the tests using Surefire.

4. Running a Group of Test Classes

If you want to run a group of test classes, you can use TestNG's test grouping feature. This allows you to categorize your tests and run them based on their group name. To do this, you will need to add the following annotation to your test classes:

@Test(groups = {"group name"})

For our CalculatorTest class, we can add the group name "calculator" to all its test methods. Then, in the Surefire configuration, we can specify the group name to run as follows:

<configuration>

<groups>calculator</groups>

</configuration>

This will run all test methods with the "calculator" group name.

5. Running Tests from Command Line

You can also run tests from the command line using Surefire and TestNG. For this, you will need to navigate to your project directory and use the following command:

mvn test

This will execute all tests in your project and generate a report.

In conclusion, Surefire and TestNG are powerful tools for running tests in your Java project. With their help, you can easily run a single test class or a group of test classes, making your testing process more efficient. So the next time you need to run tests, keep these tools in mind and give them a try.

Related Articles

Testing a JAX-RS Web Service

<strong>Testing a JAX-RS Web Service</strong> JAX-RS (Java API for RESTful Services) is a powerful framework for building RESTfu...

NoClassDefFoundError in JUnit

NoClassDefFoundError in JUnit: A Common Issue for Java Developers JUnit is a popular open-source testing framework for Java developers. It p...