TestNG Overview & Annotations

0
50

What is TestNG?

    TestNG is an automation testing framework in which NG stands for “Next Generation”. TestNG is inspired from JUnit which uses the annotations (@). TestNG overcomes the disadvantages of JUnit and is designed to make end-to-end testing easy.

Why TestNG?

    Default Selenium tests do not generate a proper format for the test results. Using TestNG we can generate test reports in HTML. 

    Multiple test cases can be grouped more easily by converting them into testng.xml file. In which you can make priorities which test case should be executed first.

    The same test case can be executed multiple times without loops just by using keyword called ‘invocation count.’

    Using TestNG, you can execute multiple test cases on multiple browsers, i.e., cross browser testing.

    The testing framework can be easily integrated with tools like Maven, Jenkins, etc.

    Annotations used in the testing are very easy to understand ex: @BeforeMethod, @AfterMethod, @BeforeTest, @AfterTest

    TestNG does not require you to have a main() method.

Advantages of TestNG over JUnit

    Annotations are easier to understand

    Test cases can be grouped more easily

    Parallel testing is possible

How to setup TestNG in project

    Install TestNG. Go to Eclipse > Install New Software > Add >

    Name:- TestNG

    Location:- http://dl.bintray.com/testng-team/testng-eclipse-release/

    Add TestNG dependency in POM file. 

<dependency>

       <groupId>org.testng</groupId>

       <artifactId>testng</artifactId>

       <version>6.14.3</version>

       <scope>test</scope>

</dependency>

TestNG Annotation

BeforeSuite – The annotated method will be run only once before all tests in this suite have run.

BeforeTest – The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.

BeforeClass – The annotated method will be run only once before the first test method in the current class is invoked.

BeforeMethod – Executes before each test method

Test – The test script

AfterMethod – Executes after each test method

AfterClass – The annotated method will be run only once after all the test methods in the current class have run.

AfterTest – The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.

AfterSuite – The annotated method will be run only once after all tests in this suite have run.

Points to be noted:-

    @Test Annotation tells that the below method is a test case.

    We can have multiple @Test annotation in a single TestNG Class. 

    The Assert class is used to conduct verification operations in TestNG.

    Text Based Report can be seen in Windows Console. 

    HTML Report can be seen in test-output folder. You need to right click on project and need to refresh.

    Priority can be added to test. 

    By default, methods annotated by @Test are executed alphabetically.

    @Test(enabled = false)helps to disable this test case

    Tests can be grouped e.g. @Test(groups = { “smoke”, “regression”})

    @DataProvider is used to provide the data to Test Method. 

    We can run suite from command prompt using below command:-

java -cp “/opt/testng-6.8.jar:bin” org.testng.TestNG testng.xml


LEAVE A REPLY

Please enter your comment!
Please enter your name here