TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities.
TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use, such as:
- JDK 5 Annotations (JDK 1.4 is also supported with JavaDoc annotations).
- Flexible test configuration.
- Support for data-driven testing (with @DataProvider).
- Support for parameters.
- Allows distribution of tests on slave machines.
- Powerful execution model (no more TestSuite).
- Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc...).
- Embeds BeanShell for further flexibility.
- Default JDK functions for runtime and logging (no dependencies).
- Dependent methods for application server testing.
TestNG is designed to cover all categories of tests: unit, functional, end-to-end, integration, etc...
TestNG is a testing framework designed to simply a broad range of testing needs, from unit testing (testing a class in isolation of the others) to integration testing (testing entire systems made of several classes, several packages and even several external frameworks, such as application servers).
Writing a test is typically a three-step process:
- Write the business logic of your test and insert TestNG annotations in your code.
- Add the information about your test (e.g. the class name, the groups you wish to run, etc...) in a testng.xml file or in build.xml.
- Run TestNG.
The concepts used in this documentation are as follows:
- A suite is represented by one XML file. It can contain one or more tests and is defined by the <suite> tag.
- A test is represented by <test> and can contain one or more TestNG classes.
- A TestNG class is a Java class that contains at least one TestNG annotation. It is represented by the <class> tag and can contain one or more test methods.
- A test method is a Java method annotated by @Test in your source.
A TestNG test can be configured by
@BeforeXXX and @AfterXXX annotations which allows to perform some Java logic before and after a certain point, these points being either of the items listed above.
The rest of this manual will explain the following:
- A list of all the annotations with a brief explanation. This will give you an idea of the various functionalities offered by TestNG but you will probably want to consult the section dedicated to each of these annotations to learn the details.
- A description of the testng.xml file, its syntax and what you can specify in it.
- A detailed list of the various features and how to use them with a combination of annotations and testng.xml.
TestNG Annotations
There are two mainly annotations in TestNG: @Configuration and @Test.
@Configuration
This annotation is used to make any method a setup or a teardown method. In addition, the @Configuration
makes unnecessary to follow any naming convention in method names. The mainly properties
that could be used are:
- afterTest - if true, the method will be run after each test.
- afterTestClass- if true, the method will be run after all the tests in the test class.
- beforeTestClass- if true, the method will be run after the test class instantiation and beforetests methods.
- beforeTestMethod- if true, the method will be run before any test method.
In the example, the method setup() will request a new bean instance before any test method.
/**
* Gets a new bean instance used during the tests.
* @throws Exception if an error occurs during the setup.
*/
@Configuration(beforeTestMethod = true)
public void setup() throws Exception {
// Gets a bean instance.
bean = getBeanRemoteInstance(SLSBExample.class, ItfExample.class);
}
@Test
This annotation is used to define a method that will be run as a test and it is not necessary follow
any naming convention. In the example, the method test00() has the annotation and it will be a
test case:
@Test
public void test00() throws Exception {
...
}
If it is necessary to disable this test, the enabled property could be used:
@Test(enabled = false)
public void test00() throws Exception {
...
}
Writing the XML configuration file:
Once the test class is defined, it is necessary to write the XML configuration file for TestNG. The
following example indicates that the tests inside the package org.objectweb.easybeans.tests.examples
must be run:
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd">
<suite name="Test Suite Example" verbose="1">
<test name="Test Example">
<packages>
<package name="org.objectweb.easybeans.tests.examples"/>
</packages>
</test>
</suite>
The TestNG XML configuration file also supports the classes/class tags that define which classes
must be run. This following example indicates that only the tests in the TestExample class must
be run:
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd">
<suite name="Test Suite Example" verbose="1">
<test name="Test Example">
<classes>
<class name="org.objectweb.easybeans.tests.examples.TestExample"/>
</classes>
</test>
</suite>