SlideShare a Scribd company logo
TestNG Framework
Introduction
Levon Apreyan
QA Automation Engineer
Test Automation Framework
What is test automation framework?
• wikipedia - A test automation framework is an integrated system that sets the rules
of automation of a specific product.
Types of test automation frameworks exists for many languages, like Java, C#, PHP,
etc..
Examples: JUnit, Mockito, TestNG, etc..
TestNG is a testing framework, that supports:
•Unit Testing
•Integration Testing
•Annotations
•Annotation Transformers
•Parameters
•Listeners
•Group testing
•Dependencies
•Etc..
What is TestNG?
TestNG vs JUnit
Why TestNG?
Annotations
@BeforeSuite, @BeforeTest, @BeforeGroups, @BeforeClass, @BeforeMethod
@AfterSuite, @AfterTest, @AfterGroups, @AfterClass, @AfterMethod
@Test
@DataProvider
@Factory
@Listeners
Annotations
@BeforeSuite - runs before all tests in this suite have run.
@BeforeTest - runs before any test method belonging to the classes inside the <test>
tag is run.
@BeforeGroups - runs shortly before the first test method that belongs to any of these
groups is invoked.
@BeforeClass - runs before the first test method in the current class is invoked.
@BeforeMethod - run before each test method.
@AfterSuite, @AfterTest, @AfterGroups, @AfterClass, @AfterMethod - the same,
but after
Annotations
@Test annotation attributes
dataProvider - The name of the data provider for test method.
groups - The list of groups this class/method belongs to.
expectedExceptions - The list of exceptions that a test method is expected
to throw.
timeOut - The maximum number of milliseconds this test should take.
etc...
Annotation Transformers
TestNG allows to modify the content of all the annotations at runtime.
An Annotation Transformer is a class that implements IAnnotationTransformer or
IAnnotationTransformer2 interface.
Annotation Transformers
Example: override the attribute invocationCount but only on the test method invoke()
of one of test classes:
public class MyTransformer implements IAnnotationTransformer {
public void transform(ITest annotation, Class testClass, Constructor testConstructor, Method
testMethod)
{
if ("invoke".equals(testMethod.getName())) {
annotation.setInvocationCount(5);
}
}
}
Parameters
Parameters can be passed to test methods.
There are two ways to set these parameters: with testng.xml or programmatically.
Parameters
Example 1: Parameters from testing.xml (for simple values only)
@Parameters({ "first-name" })
@Test
public void testSingleString(String firstName) {
System.out.println("Invoked testString " + firstName);
assert "Cedric".equals(firstName);
}
This XML parameter is defined in testng.xml:
<suite name="My suite">
<parameter name="first-name" value="Cedric"/>
Parameters
Example 2: Parameters with DataProviders
//This method will provide data to any test method that declares that its
Data Provider is named "test1"
@DataProvider(name = "test1")
public Object[ ][ ] createData1() {
return new Object[ ][ ] {
{ "Cedric", new Integer(36) },
{ "Anne", new Integer(37)},
};
}
Parameters
Example 2: Parameters with DataProviders
//This test method declares that its data should be supplied by the Data
Provider named "test1"
@Test(dataProvider = "test1")
public void verifyData1(String n1, Integer n2) {
System.out.println(n1 + " " + n2);
}
Output:
Cedric 36
Anne 37
Listeners
"TestNG Listeners" allow to modify TestNG's behavior or implement some
actions.
There are several listener interfaces.
Listeners
Here are a few listeners:
● IAnnotationTransformer - allows to modify the content of @Test annotation
● IAnnotationTransformer2 - allows to modify the content of other annotations
● IHookable - allows to override and possibly skip the invocation of test methods
● IInvokedMethodListener - allows to be notified whenever TestNG is about to
invoke a test (annotated with @Test) or configuration (annotated with any of the @Before
or @After annotation) method
Listeners
Here are a few listeners (continuation):
● IMethodInterceptor - allows control over running order of methods that don’t have
dependencies or dependents
● IReporter - will generate test report after all the suites have been run
● ISuiteListener - listener for test suites
● ITestListener - listener for test methods
Listeners
After implementing one of these interfaces, you can let TestNG know about it with either of the
following ways:
● Using -listener on the command line.
● Using <listeners> with ant.
● Using <listeners> in your testng.xml file.
● Using the @Listeners annotation on any of your test classes.
● Using ServiceLoader.
Listeners
Example 1: declaring listener with annotation:
@Listeners({ com.example.MyListener.class, com.example.MyMethodInterceptor.class })
public class MyTest {
// …
}
Listeners
Example 2: declaring annotation with ServiceLoader (which Workfront uses).
Assume we have listener - test.tmp.TmpSuiteListener
Create a file at the location META-INF/services/org.testng.ITestNGListener, which will name the
implementation(s) of listeners:
$ cat META-INF/services/org.testng.ITestNGListener
test.tmp.TmpSuiteListener
$ tree
|____META-INF
| |____services
| | |____org.testng.ITestNGListener
Test Groups
• Test methods can be grouped together.
• Test groups can contain other groups.
This allows TestNG to be invoked and asked to include a certain set of groups (or regular
expressions) while excluding another set.
Test Groups
For example, we have two categories of tests:
● Check-in tests. These tests should be run before you submit new code. They should
typically be fast and just make sure no basic functionality was broken.
● Functional tests. These tests should cover all the functionalities of your software and be run
at least once a day, although ideally you would want to run them continuously.
Test Groups
And we have following tests:
public class Test1 {
@Test(groups = { "functest", "checkintest" })
public void testMethod1() {
}
@Test(groups = {"functest", "checkintest"} )
public void testMethod2() {
}
@Test(groups = { "functest" })
public void testMethod3() {
}
}
Test Groups
Invoking TestNG with the following configuration will run all the test methods:
<test name="Test1">
<groups>
<run>
<include name="functest"/>
</run>
</groups>
<classes>
<class name="example1.Test1"/>
</classes>
</test>
Invoking it with checkintest will only run testMethod1() and testMethod2().
Test Groups
Groups can include other groups. These groups are called "MetaGroups".
<test name="Regression1">
<groups>
<define name="functest">
<include name="windows"/>
<include name="linux"/>
</define>
<define name="all">
<include name="functest"/>
<include name="checkintest"/>
</define>
<run>
<include name="all"/>
<run>
</groups>
<classes>
<class name="test.sample.Test1"/>
</classes>
</test>
Test Groups
TestNG allows to include groups as well as exclude them. For example we have a test
that is broken.
@Test(groups = {"checkintest", "broken"} )
public void testMethod2() {
}
We can set the broken tests to be excluded from run in testng.xml :
…
<groups>
<run>
<include name="checkintest"/>
<exclude name="broken"/>
</run>
</groups>
…
Test Groups
TestNG allows to define groups at the class level and then add groups at the method
level:
@Test(groups = { "checkin-test" })
public class All {
@Test(groups = { "func-test" )
public void method1() { ... }
public void method2() { ... }
}
method2() is part of the group "checkin-test".
method1() belongs to both "checkin-test" and "func-test".
Dependencies
Dependencies are for test methods to be invoked in a certain order.
TestNG allows to specify dependencies either with annotations or in XML.
Dependencies
Two types of Dependencies:
• Hard dependencies. All the dependent methods must have run and succeeded for you to
run.
• Soft dependencies. Dependent method will always be run after the methods you depend
on, even if some of them have failed. This is done by adding "alwaysRun=true" in @Test
annotation.
Dependencies
Example 1: hard dependency:
@Test
public void serverStartedOk() {}
@Test(dependsOnMethods = { "serverStartedOk" })
public void method1() {}
Dependencies
Example 2: Soft dependency:
@Test
public void serverStartedOk() {}
@Test(dependsOnMethods = { "serverStartedOk" }, alwaysRun=true)
public void method1() {}
Dependencies
We can also have methods that depend on entire groups:
@Test(groups = { "init" })
public void serverStartedOk() {}
@Test(groups = { "init" })
public void initEnvironment() {}
@Test(dependsOnGroups = { "init.*" })
public void method1() {}
In this example, method1() is declared as depending on any group matching the regular
expression "init.*".
Running TestNG
TestNG can be invoked in different ways:
● Command line
● ant
● Eclipse
● IntelliJ IDEA
Running TestNG
The simplest way to invoke TestNG from command line is as follows:
java org.testng.TestNG testng1.xml [testng2.xml testng3.xml ...]
where:
org.testng.TestNG is the main class;
testng1.xml [testng2.xml testng3.xml ...] are configuration files.
Running TestNG
We can run tests without testng.xml:
java org.testng.TestNG -groups windows,linux -testclass org.test.MyTest
where:
org.testng.TestNG is the main class;
-groups windows,linux specify which group tests should run;
-testclass org.test.MyTest specity which test clssses should run.
Logging and results
The results of the test run are created in a file called index.html.
It is possible to generate own reports with TestNG listeners and reporters:
● Listeners are notified in real time of when a test starts, passes, fails, etc…
● Reporters are notified when all the suites have been run by TestNG.
Integrating TestNG in environment
There are TestNG plugins available for the following IDEs:
• Eclipse
• IntelliJ IDEA
Plugins help to easily to:
• configure TestNG
• convert JUnit tests to TestNG tests
• run TestNG tests
• view test results
How to include TestNG in Maven or Ant
• Maven: add the following to pom.xml:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.12</version>
<scope>test</scope>
</dependency>
• Ant: define the TestNG ant task as follows:
<taskdef resource="testngtasks" classpath="testng.jar"/>
References
• http://testng.org/doc/index.html - TestNG site
• http://testng.org/doc/documentation-main.html
- Documentation
THANK YOU!
Questions?
Contact: lapreyan@gmail.com

More Related Content

PPT
PPTX
TestNG Session presented in Xebia XKE
PPTX
Automation Testing by Selenium Web Driver
PPTX
Cucumber With Selenium
PPTX
Introduction to Selenium Web Driver
PPT
QSpiders - Automation using Selenium
PDF
Rest API Automation with REST Assured
PPT
Cucumber presentation
TestNG Session presented in Xebia XKE
Automation Testing by Selenium Web Driver
Cucumber With Selenium
Introduction to Selenium Web Driver
QSpiders - Automation using Selenium
Rest API Automation with REST Assured
Cucumber presentation

What's hot (20)

PPTX
TestNG Session presented in PB
PPTX
TestNG with selenium
PPSX
PPTX
Selenium test automation
PPTX
Automation - web testing with selenium
PPTX
Test NG Framework Complete Walk Through
PDF
Selenium IDE LOCATORS
PPTX
Test automation
PPT
Test Automation Framework Designs
PPTX
Introduction of TestNG framework and its benefits over Junit framework
PPT
Selenium Automation Framework
PDF
Page Object Model and Implementation in Selenium
PDF
PPTX
UNIT TESTING PPT
PDF
SELENIUM PPT.pdf
PPTX
Introduction to Automation Testing
PPTX
Maven ppt
PPS
JUnit Presentation
PPT
Test automation using selenium
TestNG Session presented in PB
TestNG with selenium
Selenium test automation
Automation - web testing with selenium
Test NG Framework Complete Walk Through
Selenium IDE LOCATORS
Test automation
Test Automation Framework Designs
Introduction of TestNG framework and its benefits over Junit framework
Selenium Automation Framework
Page Object Model and Implementation in Selenium
UNIT TESTING PPT
SELENIUM PPT.pdf
Introduction to Automation Testing
Maven ppt
JUnit Presentation
Test automation using selenium
Ad

Viewers also liked (20)

PDF
Test ng for testers
PDF
TestNG introduction
PDF
TestNg_Overview_Config
PPTX
Test ng tutorial
PPTX
TestNG vs JUnit: cease fire or the end of the war
PDF
TestNG vs. JUnit4
PDF
Automation Testing using Selenium
PPT
Selenium ppt
PPTX
Hybrid Automation Framework
PPT
Junit and testNG
PDF
Selenium Overview
PPTX
About change & adaptation, and why after 20 years it's just the beginning
PPT
Hybrid Automation Framework Developement
PPTX
Web service testing_final.pptx
PPTX
Autoscalable open API testing
PPTX
Coherent REST API design
PPT
Page object with selenide
PPTX
Time to REST: testing web services
PDF
Heleen Kuipers - presentatie reinventing organisations
ODP
Creating a Java EE 7 Websocket Chat Application
Test ng for testers
TestNG introduction
TestNg_Overview_Config
Test ng tutorial
TestNG vs JUnit: cease fire or the end of the war
TestNG vs. JUnit4
Automation Testing using Selenium
Selenium ppt
Hybrid Automation Framework
Junit and testNG
Selenium Overview
About change & adaptation, and why after 20 years it's just the beginning
Hybrid Automation Framework Developement
Web service testing_final.pptx
Autoscalable open API testing
Coherent REST API design
Page object with selenide
Time to REST: testing web services
Heleen Kuipers - presentatie reinventing organisations
Creating a Java EE 7 Websocket Chat Application
Ad

Similar to TestNG Framework (20)

PDF
TestNG - The Next Generation of Unit Testing
PPTX
PDF
20070514 introduction to test ng and its application for test driven gui deve...
PPTX
Appium TestNG Framework and Multi-Device Automation Execution
PPTX
Selenium TestNG
PPTX
TestNG Data Binding
PDF
Automation for developers
PPTX
IT talk: Как я перестал бояться и полюбил TestNG
PDF
How To Install TestNG in Eclipse Step By Step Guide.pdf
PPTX
TestNG vs Junit
PPTX
Dev labs alliance top 20 testng interview questions for sdet
ODP
Test ng
PDF
JUnit5 Custom TestEngines intro - version 2020-06
PDF
IT Talk TestNG 6 vs JUnit 4
PDF
Quality for developers
PPTX
JUnit 5 - from Lambda to Alpha and beyond
PPT
Selenium training in chennai
PDF
The Ultimate Guide to Java Testing Frameworks.pdf
PPTX
Testing Spring Applications
TestNG - The Next Generation of Unit Testing
20070514 introduction to test ng and its application for test driven gui deve...
Appium TestNG Framework and Multi-Device Automation Execution
Selenium TestNG
TestNG Data Binding
Automation for developers
IT talk: Как я перестал бояться и полюбил TestNG
How To Install TestNG in Eclipse Step By Step Guide.pdf
TestNG vs Junit
Dev labs alliance top 20 testng interview questions for sdet
Test ng
JUnit5 Custom TestEngines intro - version 2020-06
IT Talk TestNG 6 vs JUnit 4
Quality for developers
JUnit 5 - from Lambda to Alpha and beyond
Selenium training in chennai
The Ultimate Guide to Java Testing Frameworks.pdf
Testing Spring Applications

Recently uploaded (20)

PDF
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PDF
Download FL Studio Crack Latest version 2025 ?
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
medical staffing services at VALiNTRY
PDF
Cost to Outsource Software Development in 2025
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Nekopoi APK 2025 free lastest update
PDF
Complete Guide to Website Development in Malaysia for SMEs
PPTX
Advanced SystemCare Ultimate Crack + Portable (2025)
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Designing Intelligence for the Shop Floor.pdf
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
Internet Downloader Manager (IDM) Crack 6.42 Build 41
CHAPTER 2 - PM Management and IT Context
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
Download FL Studio Crack Latest version 2025 ?
wealthsignaloriginal-com-DS-text-... (1).pdf
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Adobe Illustrator 28.6 Crack My Vision of Vector Design
medical staffing services at VALiNTRY
Cost to Outsource Software Development in 2025
Wondershare Filmora 15 Crack With Activation Key [2025
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Nekopoi APK 2025 free lastest update
Complete Guide to Website Development in Malaysia for SMEs
Advanced SystemCare Ultimate Crack + Portable (2025)
Design an Analysis of Algorithms I-SECS-1021-03
Designing Intelligence for the Shop Floor.pdf

TestNG Framework

  • 2. Test Automation Framework What is test automation framework? • wikipedia - A test automation framework is an integrated system that sets the rules of automation of a specific product. Types of test automation frameworks exists for many languages, like Java, C#, PHP, etc.. Examples: JUnit, Mockito, TestNG, etc..
  • 3. TestNG is a testing framework, that supports: •Unit Testing •Integration Testing •Annotations •Annotation Transformers •Parameters •Listeners •Group testing •Dependencies •Etc.. What is TestNG?
  • 5. Annotations @BeforeSuite, @BeforeTest, @BeforeGroups, @BeforeClass, @BeforeMethod @AfterSuite, @AfterTest, @AfterGroups, @AfterClass, @AfterMethod @Test @DataProvider @Factory @Listeners
  • 6. Annotations @BeforeSuite - runs before all tests in this suite have run. @BeforeTest - runs before any test method belonging to the classes inside the <test> tag is run. @BeforeGroups - runs shortly before the first test method that belongs to any of these groups is invoked. @BeforeClass - runs before the first test method in the current class is invoked. @BeforeMethod - run before each test method. @AfterSuite, @AfterTest, @AfterGroups, @AfterClass, @AfterMethod - the same, but after
  • 7. Annotations @Test annotation attributes dataProvider - The name of the data provider for test method. groups - The list of groups this class/method belongs to. expectedExceptions - The list of exceptions that a test method is expected to throw. timeOut - The maximum number of milliseconds this test should take. etc...
  • 8. Annotation Transformers TestNG allows to modify the content of all the annotations at runtime. An Annotation Transformer is a class that implements IAnnotationTransformer or IAnnotationTransformer2 interface.
  • 9. Annotation Transformers Example: override the attribute invocationCount but only on the test method invoke() of one of test classes: public class MyTransformer implements IAnnotationTransformer { public void transform(ITest annotation, Class testClass, Constructor testConstructor, Method testMethod) { if ("invoke".equals(testMethod.getName())) { annotation.setInvocationCount(5); } } }
  • 10. Parameters Parameters can be passed to test methods. There are two ways to set these parameters: with testng.xml or programmatically.
  • 11. Parameters Example 1: Parameters from testing.xml (for simple values only) @Parameters({ "first-name" }) @Test public void testSingleString(String firstName) { System.out.println("Invoked testString " + firstName); assert "Cedric".equals(firstName); } This XML parameter is defined in testng.xml: <suite name="My suite"> <parameter name="first-name" value="Cedric"/>
  • 12. Parameters Example 2: Parameters with DataProviders //This method will provide data to any test method that declares that its Data Provider is named "test1" @DataProvider(name = "test1") public Object[ ][ ] createData1() { return new Object[ ][ ] { { "Cedric", new Integer(36) }, { "Anne", new Integer(37)}, }; }
  • 13. Parameters Example 2: Parameters with DataProviders //This test method declares that its data should be supplied by the Data Provider named "test1" @Test(dataProvider = "test1") public void verifyData1(String n1, Integer n2) { System.out.println(n1 + " " + n2); } Output: Cedric 36 Anne 37
  • 14. Listeners "TestNG Listeners" allow to modify TestNG's behavior or implement some actions. There are several listener interfaces.
  • 15. Listeners Here are a few listeners: ● IAnnotationTransformer - allows to modify the content of @Test annotation ● IAnnotationTransformer2 - allows to modify the content of other annotations ● IHookable - allows to override and possibly skip the invocation of test methods ● IInvokedMethodListener - allows to be notified whenever TestNG is about to invoke a test (annotated with @Test) or configuration (annotated with any of the @Before or @After annotation) method
  • 16. Listeners Here are a few listeners (continuation): ● IMethodInterceptor - allows control over running order of methods that don’t have dependencies or dependents ● IReporter - will generate test report after all the suites have been run ● ISuiteListener - listener for test suites ● ITestListener - listener for test methods
  • 17. Listeners After implementing one of these interfaces, you can let TestNG know about it with either of the following ways: ● Using -listener on the command line. ● Using <listeners> with ant. ● Using <listeners> in your testng.xml file. ● Using the @Listeners annotation on any of your test classes. ● Using ServiceLoader.
  • 18. Listeners Example 1: declaring listener with annotation: @Listeners({ com.example.MyListener.class, com.example.MyMethodInterceptor.class }) public class MyTest { // … }
  • 19. Listeners Example 2: declaring annotation with ServiceLoader (which Workfront uses). Assume we have listener - test.tmp.TmpSuiteListener Create a file at the location META-INF/services/org.testng.ITestNGListener, which will name the implementation(s) of listeners: $ cat META-INF/services/org.testng.ITestNGListener test.tmp.TmpSuiteListener $ tree |____META-INF | |____services | | |____org.testng.ITestNGListener
  • 20. Test Groups • Test methods can be grouped together. • Test groups can contain other groups. This allows TestNG to be invoked and asked to include a certain set of groups (or regular expressions) while excluding another set.
  • 21. Test Groups For example, we have two categories of tests: ● Check-in tests. These tests should be run before you submit new code. They should typically be fast and just make sure no basic functionality was broken. ● Functional tests. These tests should cover all the functionalities of your software and be run at least once a day, although ideally you would want to run them continuously.
  • 22. Test Groups And we have following tests: public class Test1 { @Test(groups = { "functest", "checkintest" }) public void testMethod1() { } @Test(groups = {"functest", "checkintest"} ) public void testMethod2() { } @Test(groups = { "functest" }) public void testMethod3() { } }
  • 23. Test Groups Invoking TestNG with the following configuration will run all the test methods: <test name="Test1"> <groups> <run> <include name="functest"/> </run> </groups> <classes> <class name="example1.Test1"/> </classes> </test> Invoking it with checkintest will only run testMethod1() and testMethod2().
  • 24. Test Groups Groups can include other groups. These groups are called "MetaGroups". <test name="Regression1"> <groups> <define name="functest"> <include name="windows"/> <include name="linux"/> </define> <define name="all"> <include name="functest"/> <include name="checkintest"/> </define> <run> <include name="all"/> <run> </groups> <classes> <class name="test.sample.Test1"/> </classes> </test>
  • 25. Test Groups TestNG allows to include groups as well as exclude them. For example we have a test that is broken. @Test(groups = {"checkintest", "broken"} ) public void testMethod2() { } We can set the broken tests to be excluded from run in testng.xml : … <groups> <run> <include name="checkintest"/> <exclude name="broken"/> </run> </groups> …
  • 26. Test Groups TestNG allows to define groups at the class level and then add groups at the method level: @Test(groups = { "checkin-test" }) public class All { @Test(groups = { "func-test" ) public void method1() { ... } public void method2() { ... } } method2() is part of the group "checkin-test". method1() belongs to both "checkin-test" and "func-test".
  • 27. Dependencies Dependencies are for test methods to be invoked in a certain order. TestNG allows to specify dependencies either with annotations or in XML.
  • 28. Dependencies Two types of Dependencies: • Hard dependencies. All the dependent methods must have run and succeeded for you to run. • Soft dependencies. Dependent method will always be run after the methods you depend on, even if some of them have failed. This is done by adding "alwaysRun=true" in @Test annotation.
  • 29. Dependencies Example 1: hard dependency: @Test public void serverStartedOk() {} @Test(dependsOnMethods = { "serverStartedOk" }) public void method1() {}
  • 30. Dependencies Example 2: Soft dependency: @Test public void serverStartedOk() {} @Test(dependsOnMethods = { "serverStartedOk" }, alwaysRun=true) public void method1() {}
  • 31. Dependencies We can also have methods that depend on entire groups: @Test(groups = { "init" }) public void serverStartedOk() {} @Test(groups = { "init" }) public void initEnvironment() {} @Test(dependsOnGroups = { "init.*" }) public void method1() {} In this example, method1() is declared as depending on any group matching the regular expression "init.*".
  • 32. Running TestNG TestNG can be invoked in different ways: ● Command line ● ant ● Eclipse ● IntelliJ IDEA
  • 33. Running TestNG The simplest way to invoke TestNG from command line is as follows: java org.testng.TestNG testng1.xml [testng2.xml testng3.xml ...] where: org.testng.TestNG is the main class; testng1.xml [testng2.xml testng3.xml ...] are configuration files.
  • 34. Running TestNG We can run tests without testng.xml: java org.testng.TestNG -groups windows,linux -testclass org.test.MyTest where: org.testng.TestNG is the main class; -groups windows,linux specify which group tests should run; -testclass org.test.MyTest specity which test clssses should run.
  • 35. Logging and results The results of the test run are created in a file called index.html. It is possible to generate own reports with TestNG listeners and reporters: ● Listeners are notified in real time of when a test starts, passes, fails, etc… ● Reporters are notified when all the suites have been run by TestNG.
  • 36. Integrating TestNG in environment There are TestNG plugins available for the following IDEs: • Eclipse • IntelliJ IDEA Plugins help to easily to: • configure TestNG • convert JUnit tests to TestNG tests • run TestNG tests • view test results
  • 37. How to include TestNG in Maven or Ant • Maven: add the following to pom.xml: <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.9.12</version> <scope>test</scope> </dependency> • Ant: define the TestNG ant task as follows: <taskdef resource="testngtasks" classpath="testng.jar"/>
  • 38. References • http://testng.org/doc/index.html - TestNG site • http://testng.org/doc/documentation-main.html - Documentation