Hello Friends
Till now we have seen how a Web-driver code works and some important methods of Web-driver code. Junit has been used since many year and most of the developer still using Junit for code coverage; now we have a strong tool know as TestNG. There are many advantage of TestNG over Junit before we name lets see what is TestNG in short and later we see the small code for it.
What is TestNG
TestNG is a testing framework designed to simplify a broad range of testing needs, from unit testing (testing a class in isolation of the others) to integration testing (testing entire systems made of several classes, several packages and even several external frameworks, such as application servers).
TestNG is a testing framework designed to simplify a broad range of testing needs, from unit testing (testing a class in isolation of the others) to integration testing (testing entire systems made of several classes, several packages and even several external frameworks, such as application servers).
Writing a test is typically a three-step process
- Write the business logic of your test and insert TestNG annotations in your code.
- Add the information about your test in a testng.xml.
- Run TestNG.
Advantages of TestNG over Junit
- In Junit we have to declare @BeforeClass and @AfterClass which is a constraint where as in TestNG there is no constraint like this.
- Additional Levels of setUp/tearDown level are available in TestNG like @Before/AfterSuite,@Before/AfterTest and @Before/AfterGroup
- No Need to extend any class in TestNG.
- There is no method name constraint in TestNG as in Junit. You can give any name to the test methods in TestNG
- In TestNG we can tell the test that one method is dependent on another method where as in Junit this is not possible. In Junit each test is independent of another test.
- Grouping of testcases is available in TestNG where as the same is not available in Junit.
- Also using TestNG your selenium testcase execution can be done in parallel.
Setup
Before moving further i am assuming following things has already been setup
- Java http://www.java.com/en/download/help/download_options.xml
- Maven http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
- Eclipse http://www.eclipse.org/downloads/
- Selenium WebDriver http://docs.seleniumhq.org/download/
- TestNG http://testng.org/doc/eclipse.html#eclipse-installation
- At the time of writing documentation i am using:
- Web-driver version - 2.35
- Mozilla version - 24
- Chrome version - 30.x.x.x
- Note: Disable auto update of browser version else selenium may not work on latest Mozilla/Chrome versions.
Code
package com.webdriver.testsuite;
import java.util.List;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.shopzilla.webdriver.common.SoftAssert;
import com.shopzilla.webdriver.bizrate.common.Helper;
public class BizrateTestSuite {
Helper helper;
SoftAssert softassert;
WebDriver driver;
String url;
@BeforeClass
public void init(){
helper=new Helper();
softassert = new SoftAssert();
helper.getProperties();
driver=helper.getDriver();
}
@AfterClass
public void tearDown(){
driver.close();
}
@Test
public void testLess(){
softassert.flush();
softassert.assertTrue(100<200,"Test Less 1 failed");
softassert.assertTrue(50<200,"Test Less 2 failed");
softassert.assertTrue(90<200,"Test less 3 failed");
softassert.assertAll();
}
@Test
public void testMore(){
softassert.flush();
softassert.assertTrue(10>50,"Test more 1 failed"); // Test should fail
softassert.assertTrue(20>10,"Test more 2 failed");
softassert.assertTrue(30>5,"Test more 3 failed");
softassert.assertAll();
}
@Test
public void testEqual(){
softassert.flush();
softassert.assertTrue(10==10,"Test eq 1 failed");
softassert.assertTrue(20==20,"Test eq 2 failed");
softassert.assertTrue(30==30,"Test eq 3 failed");
softassert.assertAll();
}
}
Note:
1) To run this code simply right click on code and run as "TestNG Test"
2) Softassert are used so that if some in between test/assert failed then rest test should continue to run and we get one final report after all test finished. Softassert code will provide in later post.
Report
