TestNG annotations

Tests written with the TestNG framework need to be annotated properly in order to be recognized as tests. A Java class will contain methods, that will either be the actual tests, or methods that perform some actions needed within a test. A class or a method that represent a test, will be annotated with the @Test annotation. A method or class that has this annotation needs to have the ‘public’ visibility. Having this in mind, if the @Test annotation is placed at the class level, all the methods within the class that are ‘public’ will be considered tests, and they will execute accordingly (as long as they don’t have one of the before/after annotations described lower in this post, for which there is a specific behavior). All the other methods, that are private or protected, will not be executed. Also, for this particular case, the public methods don’t even need to have the @TestNG annotation. However if you want to add  attributes to some of the test methods, as described in this post , you will need to place the @Test attribute before each test method that will have the attributes. If you desire to place different attributes to test methods, you should put the @Test annotation next to each of these methods, otherwise, you can place the attributes within the class-level @Test annotation.

When writing tests that are grouped within the same class, you might need to have some actions occuring only once per class (for performing some sort of initialization at the begining, or performing cleanup after the tests have run),  or before and after each test method. This type of code should be grouped within a corresponding method, and annotated as follows:
@BeforeClass: this annotation should be placed before a method definition that performs actions needed at the begining, before running any test from the class.  The code written inside this method will run just once, before any other method executes. An example is initializing the browser for running the tests. The name of the method annotated should reflect the purpose of the method. Also, the method has to have the ‘public’ visibility attribute. As a best practice, this annotation and method definition should be as follows:

@BeforeClass
public void beforeClass() {
....
}

@AfterClass: this annotation should be placed before a method definition that performs actions needed after all the tests from a class have finished running (no matter whether successful or not). It should be used in conjunction with methods that perform test cleanup. An example would be a method that closes all the browser instances that were used within the tests that ran. The name of the method should reflect its purpose and it should be ‘public’. As a best practice, this annotation and the corresponding method definition should be similar to:

@AfterClass
public void afterClass() {
....
}

@BeforeMethod: this annotation should be present next to a method that performs actions needed before each test method executes, or that contains the initial steps which are common to every test method from the class. For example, you could clear the cookies before each test method executes, so that you always have a clean session for running tests. The name of the method should reflect its’ purpose and it should be ‘public’. It should look something like this:

@BeforeMethod
public void beforeMethod() {
....
}

@AfterMethod: this annotation should be placed next to a method that performs actions needed after each test method has executed.  The name of the method should reflect its’ purpose and it should also be ‘public’. It should look something like:

@AfterMethod
public void afterMethod() {
....
}

The before/after annotations and methods have to appear only once per class. There are other before/after annotations, but personally i do not find them useful, but rather difficult to use when dealing with a lot of test classes that need to be able to run both separately and together as a suite.
You are now ready to move on to the next step of this tutorial, which describes @Test attributes, and can be found here: https://iamalittletester.wordpress.com/2014/02/27/testng-test-attributes/ .

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s