October 19, 2014 | 1 Comment TestNG is a testing framework inspired from JUnit and NUnit covering a wider range of test categories: unit, functional, end-to-end, integration, etc., with more powerful and easy-to-use functionalities. It is an open source automated testing framework; where NG of TestNG means ‘Next Generation’. TestNG’s main features include: Annotation support. Support for parameterized and data-driven testing (with @DataProvider and/or XML configuration). Support for multiple instances of the same test class (with @Factory) Flexible execution model. TestNG can be run either by Ant via build.xml (with or without a test suite defined), or by an IDE plugin with visual results. Concurrent testing: run tests in arbitrarily big thread pools with various policies available (all methods in their own thread, one thread per test class, etc.), and test whether the code is multithread safe. Embeds BeanShell for further flexibility. Default JDK functions for runtime and logging (no dependencies). Dependent methods for application server testing. Distributed testing: allows distribution of tests on slave machines. TestNG simplifies the way the tests are coded. FIRST STEP : Installing TestNG plugin [1] To install TestNG plugin: Go to Help -> install new software -> provide the download link in the ‘Work with’ section – http://beust.com/eclipse/ : [2] Tap on ‘Add’ – give some name and then check the TestNG checkbox -> Click on ‘Next’ : [3] Then click on ‘Next’ -> Agree to terms and conditions an complete the installation. SECOND STEP : Creating a new project [1] Create a new java project in Eclipse : Go to ‘File’ -> ‘New’ -> ‘Java Project’ [2] Add project name and click ‘Next’ – [3] Click ‘Finish’ – [4] Right click on the ‘src’ folder in your project -> ‘New’ -> ‘Package’ : [5] Specify a package name there -> then Click ‘Finish’ : THIRD STEP : Importing TestNG Library [1] To import ‘TestNG’ plugin to your project : Right click on your project -> ‘Properties’ -> ‘Libraries’ -> select ‘TestNG’ – [2] Click ‘Next’ and then ‘Finish’ – [3] Now go to ‘Order and Export’ tab and make sure everything is checked and then tap on ‘OK’ – FOURTH STEP : Creating a TestNG Test File [1] Right-click on the “src” folder of your project -> then choose New > Other.. : [2] Click on TestNG folder and select “TestNG class” -> Click ‘Next’ : [3] ‘New TestNG class’ dialog opens up where ‘Source Folder’ , ‘Package name’ and ‘Class name’ would be specified. You can modify the ‘Class name’ there to something relevant to your project – [4] Now , if you see below , some annotations would be mentioned there which we can use in our testng file. Let us select ‘@BeforeMethod’ , ‘@BeforeTest, ‘@AfterMethod’ , ‘@AfterTest’ for now. ‘@Test’ would be taken as the default one here – [5] Click on ‘Finish’ and you can see your testng template file created – The TestNG testing framework can execute your tests in parallel. It can also execute some function just before or right after your tests. For this, such methods and test methods should be marked with special annotations @BeforeMethod, @AfterMethod and @Test. Let’s take an example : @BeforeTest System.out.println(“Setting up Tests”); @BeforeMethod System.out.println(“Starting test case”); @Test a(); @Test b(); @AfterMethod System.out.println(“Test case done”); @AfterTest System.out.println(“Everything executed”); So the way of execution here would be as follows : First @BeforeTest will be called. Then it will execute parallel as – @BeforeMethod a(); @AfterMethod and @BeforeMethod b(); @AfterMethod And at last @AfterTest will be called. What TestNG does is – It will find all the methods marked as @Test. After this, it will create two parallel threads, and will start executing each method in its own thread. Before and after each @Test, @BeforeMethod and @AfterMethod are executed in each thread. BASIC SCRIPT – Let’s take one simple java script for example with TestNg annotations . We have assigned some priorities to test cases here in the way in which we want them to be executed – package com.smriti.firsttestng; import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class FirstTestNGTestCases { @BeforeTest public void setUp() { System.out.println("Test Cases : "); } @BeforeMethod public void before() { System.out.println("Starting test case"); } @Test(priority = 0) public void testmethod1() throws InterruptedException { System.out.println("Priority 0 Test"); } @Test(priority = 1) public void testmethod2() throws InterruptedException { System.out.println("Priority 1 Test"); } @AfterMethod public void after() throws Exception { System.out.println("Finished a test case"); } @AfterTest public void finishtest() throws Exception { System.out.println("Finished all test cases"); } } Running the Script – To run the code , right click on your project and run it as a ‘TestNG Test’ – Output of this script would be – Test Cases : Starting test case Priority 0 Test Finished a test case Starting test case Priority 1 Test Finished a test case Finished all test cases Thus , the order followed is – @BeforeTest –> @BeforeMethod –> @Test(priority = 0) –> @AfterMethod –> @BeforeMethod –> @Test(priority = 1) –> @AfterMethod –> @AfterTest The main advantages of TestNG over JUnit are : Annotations are easier to use and understand. Test cases can be grouped more easily. TestNG allows us to create parallel tests. We can pass additional parameters to annotations. Annotations are strongly typed, so the compiler will flag any mistakes right away. Stay tuned.More posts coming up soon!! Share if you like : FacebookTwitterLinkedin Related