Selenium Java Training - Session 21 - TestNG
(Part 1)
TestNG
TestNG plays a major role in Test Automation Frameworks Development.
• Installing TestNG Plug-in in Eclipse IDE
Checking that TestNG is not available in Eclipse IDEGo to Testng.org > Eclipse > Plug-in Installation
> Use the latest URL referred at this locationCheck whether TestNG is installed in Eclipse IDE
• Configure the Project with TestNG JAR file
Create a new Maven Project and execute a sample Selenium ScriptConfigure the Project with
TestNG (By adding Dependency tags)
• TestNG Annotations
TestNG is an API similar to Java and Selenium WebDriverTestNG has a huge list of Annotations
and below are few annotations we need to learn for Selenium:Check the annotations list in TestNG
API@Test@BeforeMethod@AfterMethod@BeforeTest@AfterTest@BeforeSuite@AfterSuite@BeforeC
lass@AfterClassAnd many more@TestThe purpose of this annotation is to represent the methods
inside Java class as Tests.This annotation replaces main() method in traditional Java programs -
DemonstrateRun as 'TestNG Test' and view the execution results in both Eclipse IDE Console and
TestNG Results tabTestNG ReportsUnder test-output folder > index.htmlRefresh the Project and
view the TestNG report generated at 'test-output' > old > index.htmlPassing a TestFailing
@Test annotated methodsIf any @Test annotated method fails, it will be displayed as failed
test in Eclipse IDE Console, TestNG results tab and default TestNG reportsDemonstrate a
program in which the @Test annotated method is failing - Demonstrate hereSkipping a test in
TestNGthrowIf we want to manually throw any exception based on some condition, we have to use
throw - Demonstrate hereSyntax: throw new Exception();Exception is the predefined class of
JavaIf we want to manually skip any test method in TestNG, we have to use throw - Demonstrate
hereSyntax: throw new SkipException();SkipException is the predefined class of TestNGTestNG
AssertionsWe perform testing to verify whether a particular test is passed or failed. In the
similar way, TestNG provides a predefined Class 'Assert' and its predefined methods
assertEquals(), assertNotEquals(), assertTrue(), assertFalse() and fail() to verify whether
@Test annotated methods are passed or failed.assertEquals()Demonstrate a program which
uses assertEquals() to verify a failing test - Demonstrate hereDemonstrate a program which
uses assertEquals() to verify a passing test - Demonstrate hereassertTrue()Demonstrate a
program which uses assertTrue() to verify a failing test - Demonstrate hereDemonstrate a
program which uses assertTrue() to verify a passing test -
Demonstrate hereassertFalse()Demonstrate a program which uses assertFalse() to verify a
failing test - Demonstrate hereDemonstrate a program which uses assertFalse() to verify a
passing test - Demonstrate herefail()Demonstrate a program which fails a test directly -
Demonstrate hereDemonstrate a program which has multiple @Test testNG annotated tests -
Demonstrate hereA single class can have multiple tests,Create multiple Tests and executeTests
will be executed in alphabetical orderDemonstrate a program which executes the @Test
annotated methods according to their priority - Demonstrate herepriority attribute of @Test
annotation is used to prioritize the tests@Test(priority=1)Check the priority attribute in TestNG API
By,
Arun Motoori