TestNG Mastery
Pramod Dutta
Lead SDET. TestNG Mastery
TheTestingAcademy
TestNG 401
Response
TheTestingAcademy
What is TestNG?
● Easy to use testing framework written in Java.
● Designed for unit, functional, and end-to-end integration
tests.
● You can use it with the API Testing and UI Testing.
● Provides features like parallel testing, multi thread
execution and annotations support.
● TestNG supports multiple plugin and can be integrated
easily with existing frameworks.
TheTestingAcademy
Why we Need TestNG?
1. Reporting
2. Parallel Execution
3. Annotations Support
4. Multi threaded testing
5. Eazy integration
6. Open source
7. Better than JUnit Framework
TheTestingAcademy
How to Write Test in TestNG?
1. Create a Method and add your logic in that Method
2. Mark that Method as @Test
3. Add required Annotations like
a. description,
b. group,
c. priority,
d. enabled,
TheTestingAcademy
How to Write Test?
TheTestingAcademy
What is TestNG File?
1. It’s a XML format file.
2. It’s a file that contains the test configuration.
3. It allows us to organize test classes.
4. You can define test suites and tests.
5. You can add parameters and other parallel config support
here.
TheTestingAcademy
What is TestNG File?
TheTestingAcademy
TestNG Annotations
TheTestingAcademy
TestNG 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.
TheTestingAcademy
TestNG Annotations
@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.
TheTestingAcademy
TestNG Annotations
@Parameter annotation on test method is to pass
parameters to test methods.
@DataProvider annotated method is used to create
test methods or test classes at runtime with different
parameters.
@Factory can be used on a method that returns
instances of test classes or on a test class constructor in
conjunction with @DataProvider
TheTestingAcademy
TestNG Annotations
@Listeners is used at test class level to takes the array
of classes that implements a plethora of
implementations of ITestNGListener interface like
IAlterSuiteListener, IAnnotationTransformer,
IMethodInterceptor, IReporter, etc. for different
purposes.
TheTestingAcademy
Grouping of Tests.
What is the grouping of tests?
TheTestingAcademy
Running Tests in Parallel
Parallelism and thread count can be set at suite level or
test level like below.
TheTestingAcademy
Running Tests in Parallel
Methods run test methods in parallel in different threads. All
dependent methods will be run in different threads, respecting the
priority of tests.
Tests run <test> tags in parallel in separate threads.
Classes run test classes in parallel in separate threads, but test
methods in those test classes will run in the same thread.
Instances run instances of test methods/classes in parallel in
different threads.
TheTestingAcademy
Running Tests in Parallel
@DataProvider, the parallelism can
be controlled using the attribute
@DataProvider(parallel = true)
TheTestingAcademy
Listeners
Listeners are TestNG annotations that literally “listen” to the
events in a script and modify TestNG behaviour accordingly
IAnnotationTransformer
IExecutionListener
IHookable
IInvokedMethodListener
IMethodInterceptor
IReporter
ISuiteListener
ITestListener
TheTestingAcademy
ReportNG
Easy to use Reporting
<dependency>
<groupId>[Link]</groupId>
<artifactId>reportng</artifactId>
<version>1.2.2</version>
<scope>test</scope>
</dependency>
TheTestingAcademy
Listeners
Demo of IExecutionListener
TheTestingAcademy
Data Provider CSV File
<dependency>
<groupId>[Link]</gr
oupId>
<artifactId>fillo</artifactId>
<version>1.15</version>
</dependency>
User, pass x4
Passed as array to function()
TheTestingAcademy
Data Provider CSV File
TheTestingAcademy
TestNG – Parallel Test
Execution
TestNG parallel execution of tests, classes and suites with
examples. Learn how to run testng tests and suites in parallel or
single test in multiple threads.
Reduces execution time
Test Level
Class Level Allows multi-threaded tests
Method Level
Data Provider
Thread-Count
TheTestingAcademy
Why Framework
TheTestingAcademy
Why Framework
A framework defines the organization's way of doing things - a 'Single
Standard'
TheTestingAcademy
Framework vs Library
TheTestingAcademy
● [Link]
TheTestingAcademy
● [Link]
TheTestingAcademy
Modular
Modular frameworks divides the test scripts into small modules
where modules are small scripts written to perform certain tasks.
Modular framework is like creation of small, independent scripts that
represents modules, sections and functions of the application under test
individual test scripts can be combined to make larger test scripts by using a
master script to achieve the required scenarios.
Master script is used to invoke the individual modules to run end to end test scenarios
TheTestingAcademy
Modular
● [Link]
TheTestingAcademy
Keyword Driven
Keyword Driven Framework is a functional automation testing
framework that divides test cases into four different parts in order to
separate coding from test cases and test steps for better automation
TheTestingAcademy
Keyword Driven
TheTestingAcademy
Data Driven
Data driven testing allows testers to input a single test script that can execute tests for all test
data from a table and expect the test output in the same table
TheTestingAcademy
Behavior Driven
Runner File
Feature Files(Gherkin Language)
GivenWhenThen Methods/Test Scripts
TheTestingAcademy
Hybrid
Combination of multiple types
=>Data Driven+BDD+Keyword
TheTestingAcademy
Understand Requirements
● Is Framework Needed?
● Select Programming Language and tool
● Choose Your Framework Design.
● Create your framework
● Implement CI/CD
TheTestingAcademy
Framework Components
● Manage Dependencies/Projects(Maven/Gradle/PIP/NPM/Nuget)
● Manage Data(Excel/json files/prop files/xml files)
● Manage Payload and Endpoints(json strings/jsonmaps/pojos/serialize/deserialize/gson)
● Manage Tests (testng and allure for
this)(precondition/postcondition/set/config/teardown/steps,description,priority,severity/execution)
● Reuse Components(Keywords, Abstraction, Inheritance, Generics, Configs. Specs. setups/helpers)
● Logger-Report Loggers(Testng. allure), text loggers(log4J)
● Reports-Test summary, percentage, steps, description, failure reason, logs-allure
● Utils -String manipulators, Json Manipulators, Data Manipulators, Readers/Writers/custom code/tools
● CI/CT-Version Controlling-github/git, Continuous Integration and Testing-jenkins/teamcity/travisci
TheTestingAcademy
API Framework Creation
1. Folder Structure
2. Payload in the class format
3. Utils of the Excel and Fillow into the framework
4. Allure report to the framework
5. Git
6. Run via Jenkins in the local machine
TheTestingAcademy
Thanks, for attending Class
I hope you liked it.
TheTestingAcademy
Fin.
TheTestingAcademy