第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;
</