TestNG Tutorial

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 (@).
  • Using TestNG you can generate a proper report, and you can easily come to know how many test cases are passed, failed and skipped.
  • Cedric Beust is the developer of a TestNG framework.

Why we required TestNG?


  • WebDriver has no native mechanism for generating reports. Generate the report in a proper format including a number of test cases runs, the number of test cases passed, the number of test cases failed, and the number of test cases skipped.
  • 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.
  • TestNG 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


Advantages Over JUnit


  • Annotations are easier to understand
  • Test cases can be grouped more easily
  • Parallel testing is possible

Install TestNG Into eClipse

Help --> Install New Software --> Add New --> Add Below URL

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








Add TestNG Library to Project


Once TestNG installed Successfully, Restart the eclipse.

Right Click on Project --> Java Build Path --> Libraries --> Add New Library




Annotations in TestNG

TestNG Annotation is a piece of code which is inserted inside a program or business logic used to control the flow of execution of test methods.





@Test:- Marks a class or a method as a part of the test.

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


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


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


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


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


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


@BeforeMethod:- The annotated method will be run before each test method.


@AfterMethod:- The annotated method will be run after each test method.


There are few more which we should be aware about this, will have more detailed with practical in upcoming tutorials.


@BeforeGroups


The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.


@AfterGroups

The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.

@Factory

Marks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return Object[ ].

@Listeners

Defines listeners on a test class.

@Parameters

Describes how to pass parameters to a @Test method.


@DataProvider

Marks a method as supplying data for a test method. The annotated method must return an Object[ ][ ], where each Object[ ] can be assigned the parameter list of the test method. 

The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.


Benefits of Using Annotations


  • TestNG identifies the methods it is interested in, by looking up annotations. Hence, method names are not restricted to any pattern or format.
  • We can pass additional parameters to annotations.
  • Annotations are strongly typed, so the compiler will flag any mistakes right away.
  • Test classes no longer need to extend anything (such as TestCase, for JUnit 3).

~~~~ Other TestNG Related Tutorial Links~~~~

TestNG Tutorial - Introduction Part

TestNG Tutorial Advanced Topics - Part 1 (Configuration of TestNG)

TestNG Tutorial Advanced Topics - Part 2 (TestNG.XML File)

TestNG Tutorial Advanced Topics - Part 3 (Priority, Groups, Parallel Mode, invocationCount, ThreadPool)

TestNG Tutorial Advanced Topics - Part 4 (Parameterization, Data Provider)


TestNG Tutorial Advanced Topics - Part 5 (Exclude,Listeners,DependsOnMethod)

No comments:

Post a Comment