What is TestNG?
Advantages of TestNG over JUnit :
There are three major advantages of TestNG over JUnit:
Test case writing using testNG :
Annotations in TestNG :
Example Code:
- TestNG is a testing framework developed in the lines of JUnit and NUnit. It introduces some new functionalities that make it more powerful and easier to use.
- TestNG is an open source automated testing framework; where NG means Next Generation.
- TestNG is designed to cover all categories of tests − unit, functional, end-to-end, integration, etc. and it requires JDK 5 or higher.
This tutorial provides a good understanding of TestNG framework that is needed to run selenium test to deliver it with robustness and reliability.
Advantages of TestNG over JUnit :
There are three major advantages of TestNG over JUnit:
- Annotations are easier to understand.
- Test cases can be grouped more easily.
- Parallel testing is possible.
- Webdriver has no native mechanism for generating reports so TestNG can generate reports based on our Selenium test results.
- There is no more need for a static main method in our tests. The sequence of actions is regulated by easy-to-understand annotations that do not require methods to be static.
- Uncaught exceptions are automatically handled by TestNG without terminating the test prematurely. These exceptions are reported as failed steps in the report.
Test case writing using testNG :
Writing a test in TestNG is quite simple and basically involves following steps:
Step 1 – Write the business logic of the test
Step 2 – Insert TestNG annotations in the code
Step 3 – Add the information about your test (e.g. the class names, methods names, groups names etc…) in a testng.xml file
Step 4 – Run TestNG
Annotations in TestNG :
@BeforeSuite: The annotated method will be run before all tests in this suite have run.
@AfterSuite: The annotated method will be run after all tests in this suite have run.
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run.
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the tag have run.
@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.
@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
@AfterClass: The annotated method will be run after all the test methods in the current class have been run.
@BeforeMethod: The annotated method will be run before each test method.
@AfterMethod: The annotated method will be run after each test method.
@Test: The annotated method is a part of a test case.
Example Code:
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | import org.testng.annotations.AfterClass; import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterSuite; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeSuite; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class NewTest { @Test public void testCase1() { System.out.println("This is the Test Case 1"); } @Test public void testCase2() { System.out.println("This is the Test Case 2"); } @BeforeMethod public void beforeMethod() { System.out.println("This will execute before every Method"); } @AfterMethod public void afterMethod() { System.out.println("This will execute after every Method"); } @BeforeClass public void beforeClass() { System.out.println("This will execute before the Class"); } @AfterClass public void afterClass() { System.out.println("This will execute after the Class"); } @BeforeTest public void beforeTest() { System.out.println("This will execute before the Test"); } @AfterTest public void afterTest() { System.out.println("This will execute after the Test"); } @BeforeSuite public void beforeSuite() { System.out.println("This will execute before the Test Suite"); } @AfterSuite public void afterSuite() { System.out.println("This will execute after the Test Suite"); } } |
It is very informative and useful.Thanks.
ReplyDeleteThank you for giving this best information. It’s a very nice topic
ReplyDeleteYou are doing a good job and sharing your knowledge to others! it was one of the good post to read and useful to improve the knowledge as updated one, keep doing the good work.
ReplyDeleteSelenium Training in Electronic City, Bangalore my knowledge as updated one, keep blogging.
This comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by the author.
ReplyDelete