This post will discuss TestNG and how to execute our first test case. A video tutorial is also provided at the end of the post for reference.
Note: We have also attached a video at the end of this article. In case you are more comfortable with the video version, then please feel free to have a look
What is TestNG?
TestNG is a testing framework inspired by JUnit and NUnit but introduces some new functionalities that make it more powerful and easier to use, such as:
- It provides support for annotations, which makes writing tests very easy.
- Test whether our code is multithread safe or not.
- Flexible test configuration.
- Supports parallel testing.
- Support for data-driven testing (with @DataProvider).
- Support for parameters.
- It is supported by various tools and plugins (Eclipse, IDEA, Maven, etc.). If you want to configure the TestNG plugin in eclipse, you can read this article; it will surely help you install TestNG in Eclipse.
TestNG is an open-source testing framework where NG stands for Next generation, designed to simplify a broad range of testing needs, from unit testing to integration testing.
How to run your first test case?
We will follow the below steps to execute our first test case –
- Create a Maven project ( please read this article on how to create a Maven project )
- Add the TestNG maven dependency to the project.
- Create a Java class and write a test case
- Execute that test case using TestNG.
We have already created a Maven Project, so let’s add the TestNG Maven dependency to it.
Add TestNG dependency
Add the below dependency in the project’s pom.xml file
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.6.1</version>
<scope>test</scope>
</dependency>
We can choose to remove the <scope>test</scope>
line from TestNG dependency. This will expand the TestNG features to the src/main/java folder too. Otherwise, if we keep <scope>test</scope>
line, TestNG features can only be leveraged inside the src/test/java folder.
Write Test Cases
Create a new Java class(“CodekruTest”) inside the src/main/java folder.
Make a method inside the class and annotate that method with @Test annotation. @Test annotation is provided by TestNG to convert a method into a test case. So, the methods annotated with @Test annotation will become test methods and will be available to TestNg for test execution
We will create a single method (“test1”) and annotate it with @Test annotation.
import org.testng.annotations.Test;
public class CodekruTest {
@Test
public void test1(){
System.out.println("Executing test1");
}
}
We can now execute the test case by right-clicking on the class and select “Run”.
This will execute the test case and you will see the print statement printed in the console.
Executing test1
===============================================
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
Video Tutorial
This is it. We hope that you liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.