Tuesday 22 January 2013

TestNG Basic Concepts and Annotations


TestNG engine supports a series of annotations, with these annotations it even has stronger flexibility and extensibility than Junit, we will learn these annotations from this tutorial,  first of all, let us take a quick view the life cycle of a typical TestNG case.
From the given illustration, we know that the life-cycle of a TestNG case starts with @BeforeClass and ends with@AfterClass@BeforeClass/@AfterClass methods will be run before/after any method in a given is run, they are designed for those expensive resource initialization/cleanup and recovery, we didn’t put @BeforeSuite, @BeforeGroups, @AfterGroups and @AfterSuite to this illustration, but if they were, they will be ran even before @BeforeClass or after @AfterClass@Configuration is deprecated so we don’t recommend use it.

TestNG Basic Annotations for configuration methods

PriAnnotation nameDocumentation
1@BeforeSuiteAnnotates methods that will be run before any method in a given is run.
2@BeforeGroupsAnnotates methods that will be run before the first method in any of the specified groups is run.
3@BeforeClassAnnotates methods that will be run before the first method on the current test class is run.
4@BeforeTestAnnotates methods that will be run before any method in a given is run.
5@BeforeMethodAnnotates methods that will be run before each test method.
6@AfterMethodAnnotates methods that will be run after every test method.
7@AfterTestAnnotates methods that will be run after all the test methods in a given have been run.
8@AfterClassAnnotates methods that will be run after the last test method on the current class is run.
9@AfterGroupsAnnotates methods that will be run after the last test method belonging to the groups specified in its value attribute has been run. The annotated method is automatically put into these specified groups.
10@AfterSuiteAnnotates methods that will be run after all the test methods in a given have been run.
The annotations @Test annotates a method as test case in TestNG pattern.

One example with TestNG Annotations

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.asjava;
import org.testng.annotations.*;
public class TestNGTest {
    @BeforeGroups
    public void BeforeGroups() {
        System.out.println("@BeforeGroups");
    }
    @BeforeClass
    public void BeforeClass() {
        System.out.println("@BeforeClass");
    }
    @Test(groups = {"My group"})
    public void test1() {
        System.out.println("test1");
    }
    @Test
    public void test2() {
        System.out.println("test2");
    }
    @AfterClass
    public void AfterClass() {
        System.out.println("@AfterClass");
    }
    @AfterMethod
    public void AfterMethod() {
        System.out.println("@AfterMethod");
    }
}
Results:
[Parser] Running:
C:\Users\Administrator\.IntelliJIdea70\system\temp-testng-customsuite.xml
@BeforeClass
test1
@AfterMethod
test2
@AfterMethod
@AfterClass
===============================================
Custom suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

No comments:

Post a Comment