2. Agenda
TestNG Overview
TestNG Plugins for Eclipse
TestNG Maven Dependency
TestNG Annotations
TestNG XML
TestNG with Selenium
TestNG Parameters and Data Provider
TestNG Parallel Execution and Cross Browser Testing
3. TestNG Overview
TestNG is a testing framework introducing some new functionality that make
it more powerful and easier to use.
It is an open source automated testing framework hence no cost required for
implementation.
It is designed to cover all categories of tests: unit, functional, end-to-end,
integration, etc.
4. TestNG Overview … continued
Based on annotations (either Javadoc or JDK5)
Flexible runtime configuration (XML, not Java)
Introduces the notion of “test groups” to clearly separate static testing
(implementing the methods) from dynamic (which tests are run)
Dependent test methods, parallel testing, load testing, partial failure
Flexible plug-in API (report creation, even changing the core behavior)
Created by Dr. Cédric Beust (of Google)
7. TestNG Annotations
@BeforeSuite: Run before all tests in this suite have run
@AfterSuite: Run after all tests in this suite have run
@BeforeTest: Run before any test method belonging to the class
@AfterTest: Run after all the test methods belonging to the class
@BeforeGroups: Run before any test method belonging to the group
@AfterGroups: Run after all test methods belonging to the group
8. TestNG Annotations … continued
@BeforeClass: Run before the first test method in the current class
@AfterClass: Run after all the test methods in the current class
@BeforeMethod: Run before each test method
@AfterMethod: Run after each test method
@Test: Identify a test method
@DataProvider: Create parameters to pass to your test methods
@Factory: Create your own test objects at runtime
9. TestNG Annotations … continued
@Test: Identify a test method
groups: The groups this method belongs to
parameters: The parameters that will be passed to your test method, as they are
found in testng.xml
dependsOnGroups: The list of groups this test method depends on.
dependsOnMethods: The list of groups this test method depends on.
enabled: Use to ignore a test
timeOut: How long TestNG should wait before declaring your test method has
failed
10. TestNG Annotations … continued
@Test(enabled=true,
groups = { “sanity” },
timeOut = 10000)
public void test1() {
// …
}
11. TestNG Annotations … continued
@Test(enabled=true,
groups = { “regression” },
timeOut = 10000,
dependsOnGroups = { “sanity” })
public void test2() {
// …
}
12. TestNG Annotations … continued
@Test(enabled=true,
dependsOnMethod = { “test2” })
public void test3() {
// …
}
13. TestNG XML
Where all the runtime information goes:
The test methods, classes, packages
Which groups should be run (include-groups)
Which groups should not be run
(exclude-groups)
Define additional groups (“groups of groups”)
Whether the tests should be run in parallel
Parameters
14. TestNG XML … continued
<test name=“Simple">
<groups>
<run>
<include name=“functest”/>
</run>
</groups>
<classes>
<class name=“SimpleTest” />
</classes>
</test>
18. TestNG Listeners
There are many types of listeners available in TestNG for
example: IAnnotationTransformer, IAnnotationTransformer2, IConfigurable,
IConfigurationListener, IConfigurationListener2, IExecutionListener, IHookable,
IInvokedMethodListener, IInvokedMethodListener2, IMethodInterceptor,
IReporter, ISuiteListener, ITestListener.
19. TestNG Listeners … continued
ISuiteListener: It has two method in it onStart() & onFinish(). Whenever a
class implements this listener, TestNG guarantees the end-user that it will
invoke the methods onStart() and onFinish() before and after running a
TestNG Suite. So before TestNG picks up your suite for execution, it first
makes a call to onStart() method and runs whatever has been scripted in this
method. In a similar way, it again makes a call to onFinish() method after a
suite has been run.
20. TestNG Listeners … continued
ITestListener: The working of this listener is also exactly the same as
ISuiteListerner but the only difference is that it makes the call before and
after the Test not the Suite. It has seven methods in it.
onFinish(): Invoked after all the tests have run and all their Configuration
methods have been called.
onStart(): Invoked after the test class is instantiated and before any
configuration method is called.
21. TestNG Listeners … continued
onTestFailedButWithinSuccessPercentage(ITestResult result): Invoked each
time a method fails but has been annotated with successPercentage and this
failure still keeps it within the success percentage requested.
onTestFailure(ITestResult result): Invoked each time a test fails.
onTestSkipped(ITestResult result): Invoked each time a test is skipped
onTestStart(ITestResult result): Invoked each time before a test will be
invoked.
22. TestNG Listeners … continued
onTestSuccess(ITestResult result): Invoked each time a test succeeds.
IInvokedMethodListener: The working of this listener is also exactly the same
as ISuiteListerner & ITestListerner and the only difference is that it makes the
call before and after every Method. It has only two methods in it.
afterInvocattion(): Invoke after each method
beforeInvocation(): Invoke before each method