第1章 Introduction
The TestNG project aims to help developerstake the next step—the NG stands for “next generation”—enabling broader anddeeper test coverage that encompasses not only unit tests but also acceptance,functional, and integration tests.
· TestNG is a testing framework written by Java and inspired from JUnit andNUnit, it is not only inherited existing functions from Junit as well as introducing some new functionality that make it more powerful and easier touse, to get more reliable, maintainable and testable codes.
Writing a test is typically a three-stepprocess:
· Write the business logic of your test and insertTestNGannotations in your code.
· Add the information about your test (e.g. theclass name, the groups you wish to run, etc...) in a testng.xmlfile or in build.xml.
· Run TestNG.
The concepts used in this documentation areas follows:
· A suite is represented by one XML file. It cancontain one or more tests and is defined by the <suite> tag.
· A test is represented by <test> and cancontain one or more TestNG classes.
· A TestNG class is a Java class that contains atleast one TestNG annotation. It is represented by the <class> tag and cancontain one or more test methods.
· A test method is a Java method annotated by @Testin your source.
第2章 Download and installation
2.1 Pre-Requirement
TestNG requires JDK 5or higher.
2.2 TestNG Download and installation
You can download the current release versionof TestNG here http://testng.org/doc/download.html, Unpacking the zipdistribution to get TestNG decompression file, which includes the below subfiles:
· Testng-5.14.1.jar(pleaseadd to your project directly, you may also not be able to successfully buildyour codes with this jar because TestNG official decided the releasedid not include all external jar files in order to keep the size down.)
· Doc(TestNGtutorial material).
· Examplecodes.
· Testng sourcecodes.
· Readme.
As a beginner, I highly recommend You startand write TestNG from example codes and docs(best materials).
For the Eclipse plug-in, we suggest using theupdate site:
· For Eclipse 3.4 and above, enter http://beust.com/eclipse.
· For Eclipse 3.3 and below, enterhttp://beust.com/eclipse1.
第3章 Annotations
@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. |
@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. |
@Test | Marks a class or a method as part of the test. |
|
|
|
|
第4章 TestNG Tutorial
4.1 Basic Usage
importjava.util.*;
importorg.testng.Assert;
importorg.testng.annotations.*;
publicclass TestNGTest1 {
privateCollection collection;
@BeforeClass
publicvoid oneTimeSetUp(){
// one-timeinitialization code
System.out.println("@BeforeClass - oneTimeSetUp");
}
@AfterClass
publicvoid oneTimeTearDown(){
// one-timecleanup code
System.out.println("@AfterClass - oneTimeTearDown");
}
@BeforeMethod
publicvoid setUp(){
collection =newArrayList();
System.out.println("@BeforeMethod - setUp");
}
@AfterMethod
publicvoid tearDown(){
collection.clear();
System.out.println("@AfterMethod - tearDown");
}
@Test
publicvoidtestEmptyCollection(){
Assert.assertEquals(collection.isEmpty(),true);
System.out.println("@Test - testEmptyCollection");
}
@Test
publicvoidtestOneItemCollection(){
collection.add("itemA");
Assert.assertEquals(collection.size(),1);
System.out.println("@Test - testOneItemCollection");
}
}
Result
@BeforeClass - oneTimeSetUp
@BeforeMethod - setUp
@Test - testEmptyCollection
@AfterMethod - tearDown
@BeforeMethod - setUp
@Test - testOneItemCollection
@AfterMethod - tearDown
@AfterClass - oneTimeTearDown
PASSED: testEmptyCollection
PASSED: testOneItemCollection
4.2 Marks Test
If you would like to mark a test method or class as testcase, you can do it as below:
The @Test annotation can be put on a class instead of a testmethod:
@Test
public class Test1 {
public void test1() {
}
public void test2() {
}
}
The effect of a class level @Test annotation is to make allthe public methods of this class to become test methods even if they are notannotated. You can still repeat the @Test annotation on a method if you want toadd certain attributes.
For example:
@Test
public class Test1 {
public void test1() {
}
@Test(groups = "g1")
public void test2() {
}
}
will make both test1() and test2() test methods but on topof that, test2() now belongs to the group "g1".
4.3 Ignore Test
If you don’t want to run a test case, you can ignore it asbelow:
importorg.testng.annotations.*;
/**
* TestNG Ignore Test
* @author mkyong
*
*/
publicclass TestNGTest3 {
@Test(enabled=false)
publicvoid divisionWithException(){
System.out.println("Method is not ready yet");
}
}
In above example, TestNG will not test the divisionWithException() method.
4.4 Dependence Test
If a test case depends on another test case or method, wecan uses “dependOnMethods“ provided by TestNG to implement the dependency testing asfollowing:
importorg.testng.annotations.*;
/**
* TestNG Dependency Test
* @author mkyong
*
*/
publicclass TestNGTest7 {
@Test
publicvoid method1(){
System.out.println("This is method 1");
}
@Test(dependsOnMethods={"method1"})
publicvoid method2(){
System.out.println("This is method 2");
}
}
Result
PASSED: method1
PASSED: method2
The “method2()” will execute only if “method1()” isrun successfully, else “method2()” will skip.
If you would like to run method2(), no mattermethod1() success or not, we can do it like this:
@Test(dependsOnMethods={"method1"},alwaysRun = true)
4.5 Test Groups
We can specify a test group in a test method, which can beinclude or exclude in textng.xml to launch:
@Test(groups = { "end2endtestSmart", "MediumLevel" }, enabled = true)
@Attemps(times = 2)
publicvoid downloadFreeApplication() throws Exception {
//Given we're going to download a free application named "Vuvuzela".
end2EndTestData.setDefaultEntryUrl("http://msmart.proxy.drutt.com/");
end2EndTestData.setAppName("Vuvuzela");
end2EndTestData.setAppId("mobileweaver-1211-1281805376266");
end2EndTestData.setAppType(AppType.FREE);
super.downloadApplicationFromEstore(end2EndTestData);
}
4.6 An Example from project
package com.test.mm.end2end.cases;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.test.mm.end2end.actions.End2EndTestAction;
import com.test.mm.end2end.data.End2EndTestData;
publicclass End2EndTestBase {
private End2EndTestAction end2EndTestAction;
@BeforeClass(alwaysRun = true)
publicvoid setUp() {
}
@AfterClass(alwaysRun = true)
publicvoid tearDown(){
//Finally recover test environment: clear the data generated by purchaseand download
end2EndTestAction.clearDownloadedData();
}
@AfterMethod(alwaysRun = true)
publicvoid recoverEnvironment(){
//Finally recover test environment: clear the data generated by purchaseand download
end2EndTestAction.destroyWebDriver();
}
/**
* @Description An End User purchases and downloadsan one-time purchaseapplication from mobile site successfully
*/
@Test(groups = { "end2endtest", "HighLevel" }, enabled = true)
publicvoid downloadApplicationFromEstore(End2EndTestData end2EndTestData) throws Exception {
//Given we create a end2EndTestAction with given end2EndTestData for specifictest case
end2EndTestAction = new End2EndTestAction (end2EndTestData);
//When the end user access estore wap site and login, e.g.:http://tswap.proxy.drutt.com/
end2EndTestAction.loginEstoreMobileSite();
//And when the end user search the application named"oneTimePurchaseApplication"
end2EndTestAction.searchAnApplication();
//And when the end user click to purchase and download theapplication
end2EndTestAction.clickToDownloadTheApllication();
//Then the applications is downloaded to the phone and is addedunder My Store
end2EndTestAction.findDownloadedAppInMyStore();
//Click to rate the downloaded application
end2EndTestAction.rateDownloadedApplication();
//Click to re-download the downloaded application
end2EndTestAction.redownloadApplication();
}
/**
* @User_Story_ID
* @Test_Title An End User rents and downloads atime limited application for 7 days from mobile site successfully
* @Description
*/
@Test(groups = { "end2endtestsmart1", "MediumLevel" }, enabled = false)
publicvoid rentAndDownloadTimeLimitedApplication() throws Exception {
//Given we're going to buy an application named "CholesterolTracker".
end2EndTestData.setAppName("Cholesterol Tracker");
end2EndTestData.setAppType(AppType.RENT);
//When we rent to download and verify the application
super.downloadApplicationFromEstore(end2EndTestData);
}
}