TestNG - ilya-khadykin/notes-outdated GitHub Wiki
TODO:
TestNG, where NG stands for "next generation" is a test automation framework inspired by JUnit (in Java) and NUnit (in C#). It can be used for unit, functional, integration, and end-to-end testing. TestNG has gained a lot of popularity within a short time and is one of the most widely used testing frameworks among Java developers. It mainly uses Java annotations to configure and write test methods.
TestNG was developed by Cedric Beust. He developed it to overcome a deficiency in JUnit. A few of the features that TestNG has over JUnit 4 are:
- Extra Before and After annotations such as Before/After Suite and Before/After Group
- Dependency test
- Grouping of test methods
- Multithreaded execution
- In-built reporting framework
In TestNG, suites and tests are configured or described mainly through XML files. By default, the name of the file is testng.xml, but we can give it any other name if we want to.
TestNG allows users to do test configuration through XML files and allows them to include (or exclude) respective packages, classes, and methods in their test suite. It also allows users to group test methods into particular named groups and to include or exclude them as part of the test execution.
Parameterization of test methods is very easy using TestNG and it also provides an easy method of creating data-driven tests.
TestNG exposes its API which makes it easy to add custom functionalities or extensions, if required.
- Multiple Before and After annotation options
- XML-based test configuration and test suite definition
- Dependent methods
- Groups/group of groups
- Dependent groups
- Parameterization of test methods
- Data-driven testing
- Multithreaded execution
- Better reporting
- Open API
testng.xml
is a configuration file for TestNG. It is used to define test suites and tests in TestNG. It is also used to pass parameters to test methods.
<suite name="First Suite" verbose="1" >
<test name="First Test" >
<classes>
<class name="test.FirstTest" />
</classes>
</test>
</suite>
Run TestNG suite from CLI:
java -cp "/opt/testng-6.8.jar:bin" org.testng.TestNG testng.xml
Run TestNG suite from CLI using Maven:
Add the following to the maven-surfire-plugin in pom.xml
:
<suiteXmlFiles>
<suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
</suiteXmlFiles>
Example:
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</build>
Run with:
mvn clean install test -DsuiteXmlFile=testngSuite.xml