Learn and Implemnent TestNG

4
What is testNG Testng is a test automation tool, it can be easily integrated with selenium to create a framework. Testers requires frameworks for proper reports, handle checkpoints, or exception handling. Testng creates reports in XML and HTML formats. It also allows QA team to execute the tests in a unit and a group 1 . Why TestNG TestNG is specially designed to cover all types testing categories like Unit, Functional testing, Integration testing, End-to-end etc. It can be integrated with the build process using ANT and MAVEN build tools. After execution TestNg generates the reports and these reports can be customized according to the user’s requirements 1 . TestNG consist of power of annotations which can be added before a method to drive that method when and how it will execute 2 . @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 <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. @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.

description

Learn and implement TestNG

Transcript of Learn and Implemnent TestNG

Page 1: Learn and Implemnent TestNG

What is testNG

Testng is a test automation tool, it can be easily integrated with selenium to create a framework. Testers requires frameworks for proper reports, handle checkpoints, or exception handling. Testng creates reports in XML and HTML formats. It also allows QA team to execute the tests in a unit and a group1.

Why TestNG

TestNG is specially designed to cover all types testing categories like Unit, Functional testing, Integration testing, End-to-end etc. It can be integrated with the build process using ANT and MAVEN build tools. After execution TestNg generates the reports and these reports can be customized according to the user’s requirements1.

TestNG consist of power of annotations which can be added before a method to drive that method when and how it will execute2.

@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 <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.

@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.

@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.

Page 2: Learn and Implemnent TestNG

@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.

Let’s get started with TestNG!

Before getting started, install these software's

(If you don't have):

1. JDK2. Eclipse3. TestNG: Download TestNG from Eclipse's Help menu.4. Download Selenium server jar from http://docs.seleniumhq.org/download/

Steps to follow:Step 1: Use Eclipse. Create a new Java project.

Step 2: Add Selenium server jar as external jar file to your project. To do this. Right click on any file/folder in the project in Eclipse. Select Build Path -> Configure Build Path. Select 'Libraries' tab. Click on 'Add external jar'. Select the Selenium server jar file you have previously downloaded.

Step 3: Use TestNG library in your project: Right click on any file/folder in the project in Eclipse. Select Build Path -> Configure Build Path. Select 'Libraries' tab. Click on 'Add Library'. Select TestNG. Click 'Ok'.

Step 4: A TestNG test suite contains a number of tests. A TestNG 'test' has one or more classes, and a TestNG class consists of multiple 'Test Methods'.

For example: - Add a new Java class 'TestClass' to the project. Following is a set of test cases written as TestNG methods. ################################################################################## public class TestClass {

@BeforeClass public static void setup() { // Setup method contains all the pre-requiestes System.Out.Println("Before class executed")} @Testvoid Testcase1() {

Page 3: Learn and Implemnent TestNG

System.Out.Println("Testcase1 executed")} @Test void Testcase2() { System.Out.Println("Testcase2 executed")}

@Testvoid Testcase3() {System.Out.Println("Testcase3 executed")}

@Testvoid Testcase4() {System.Out.Println("Testcase4 executed")} @AfterClasspublic static void teardown() {// Teardown method contains cations to close server, browser or any other setup to be closed //before report generationSystem.Out.Println("AfterClass executed")}}

##################################################################################

Step 5: Add following TestNG testsuite to your project as 'TestNG.xml'

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="My sample Suite"> <test name="Search test"> <classes> <class name="TestClass" /> </classes> </test> </suite>

Step 6: Execute above TestNG testsuite by right clicking on the TestNG.xml and selecting Run as -> TestNG suite. The testcases are executed and the summary of test execution is shown in Eclipse's console. TestNG automatically generates report for your test execution. To see the report, go to your project directory, and select test-output folder. Open index.html.

Page 4: Learn and Implemnent TestNG

References

1. http://www.faichi.com/blog/automated-selenium-testing-framework-using-testng-webdriver

2. http://testng.org/doc/documentation-main.html