|
Helping ordinary people create extraordinary websites! |
TestNG Makes Java Unit Testing a BreezeBy Filippo Diotalevi2005-03-22
Configuration methods With TestNG, you can specify more than just test methods; it's also possible to specify other particular methods in a class, called configuration methods, using the dedicated annotation @Configuration. There are four types of configuration methods: • beforeTestClass methods are executed after the instantiation of the class but before any test method has been run • afterTestClass methods are executed after every test method in a class has been run • beforeTestMethod methods are executed before the execution of any test method in a class • afterTestMethod methods are executed after the execution of every test method in a class The lifecycle of a test class is further illustrated in Figure 2. Figure 2. Lifecycle of a test class
Listing 5 illustrates some examples of configuration methods. Please note that if you use groups, configuration methods must also belong to a group. Furthermore, the four types of configuration methods are not mutually exclusive, so it is possible to define methods that belong to one or more of these categories at the same time (see the aroundTestMethods() method in Listing 5 for an example). Listing 5. Examples of configuration methods @Configuration(beforeTestClass = true, groups = {"tests.workflow"})
Configuration methods in TestNG are a more powerful version of JUnit's setUp() and tearDown() methods; their main purpose is to create the right execution context for a test and to refresh data after the execution of a test case. Tutorial Pages: » Try this testing framework for its advances over JUnit » About the code » TestNG quickstart » Defining test groups » Configuration methods » Exception checking » Wrapping up » Resources First Published on IBM DeveloperWorks |
|