DEV Community

Cover image for Getting started with TestNG Java Framework
Anne Quinkenstein
Anne Quinkenstein

Posted on • Edited on

Getting started with TestNG Java Framework

  1. Install TestNG
  2. Basic Test-Annotation
  3. Create an XML
  4. Refere to packages
  5. Before/ After - Annotations
    1. xml-Level
    2. Class-Level
  6. run specific Tests with
  7. with a little help of attributes
    1. Order Methods
    2. Skip Test
    3. TimeOut
  8. Parametrizing
    1. in XML file (global environment variables)
      1. in xml
      2. in java-class
    2. with DataProvider Annotation
  9. ITestListener Interface
  10. Run Tests parallel
  11. Reporting

Why TestNG?

Testframework influnced by JUnit

  • Annotations
  • Assertions
  • Attributes (priority, dependsOnMethods, and dependsOnGroups)
  • Data-driven testing
  • Cross-browser testing

Install TestNG

in your IDE
https://testng.org/doc/

Basic Test-Annotation

@Test followed by a Method to write a test:

public class Basics { @Test public void Demo(){ System.out.println("hello"); } @Test public void Second(){.... } 
Enter fullscreen mode Exit fullscreen mode

multiple Testcases in one Class with each @Test Annotation

Create an XML

(IntelliJ XML Plugin):
xml Hierachy: Test Cuit -> Test Folder (Shell)-> Test Cases

  • to compose different Testexecutions
  • exclude/include specific tests in the execution
  • RegEx Wildcard .* (use Name Conventions!)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test name="basics"> <classes> <class name="TestNG.Basics"/> <class name="TestNG.day3"/> </classes> </test> <test name="later in the day"> <classes> <class name="TestNG.day2"> <methods> <include name="SecondTest"/> </methods> </class> </classes> </test> <test name="later"> <classes> <class name="TestNG.day3" > <methods> <exclude name="Demo*"/> </methods> </class> </classes> </test> </suite> 
Enter fullscreen mode Exit fullscreen mode

Refere to packages

Image of Package in Code
e.g. for Regressiontests

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Packages"> <test name="basics"> <packages> <package name="TestNG"/> </packages> </test> </suite> 
Enter fullscreen mode Exit fullscreen mode

Before/ After - Annotations

Before: First Execution to wipe System clean, delete Data or execute Testdata in Database. API: BaseURl put in Test, Mobile: start appium automation server
After: Last Execution to delete Cookies, stop processes, read reports, APIs: close connections

xml-Level

@BeforeTest Before Folder (<test> .... </test>)
@AfterTest After Folder (<test> .... </test>)
Scope: Folder in which it is defined

@BeforeSuite Before everthing which is in xml file (<suite> .... </suite>)
@AfterSuite After everthing which is in xml file (<suite> .... </suite>)
Scope: xml

Class-Level

@BeforeMethod Before each and every Method (e.g. Delete Cookies, REST API- Authenticate each + every time)
@AfterMethod After each and every Method
Scope: class only(runs several times for each method)

write the annotation in any class in the folder (<test> .... </test>)

@BeforeClass
@AfterClass

public class Basics { @Test public void Demo(){ System.out.println("hello"); } @BeforeTest public void prerequisite() { System.out.println("first execution"); } @AfterTest public void lastExecution(){ System.out.println("execute last"); } } 
Enter fullscreen mode Exit fullscreen mode

run specific Tests with groups

to run a specific subset of tests spread through the classes
e.g. Smoke Tests

<suite name="Suite"> <test name="basics"> <groups> <run> <include name="smoke"/> </run> </groups> <classes> <class name="TestNG.Basics"/> <class name="TestNG.day2"/> <class name="TestNG.day3"/> </classes> </test> </suite> 
Enter fullscreen mode Exit fullscreen mode

exclude is possible too

with a little help of attributes

Order Methods

TestNG executes the tests in alphabetic Order
Tweak order with ... @Test(dependsOnMethods = {"Demo", "Demo2"})

Skip Test

if you already reported a Bug and can go on
@Test(enabled = false)

TimeOut

in ms
@Test(timeOut = 4000)

Parametrizing

in XML file (global environment variables)

in xml
<suite name="Suite"> <parameter name="URL" value="qaclickacademy.com" /> <parameter name="APIKey/usrname" value="123" /> <test name="basics"> <parameter name="URL" value="basic.com" /> <classes> <class name="TestNG.Basics"/> <class name="TestNG.day3"/> </classes> </test> <test name="later in the day"> <parameter name="URL" value="day.com" /> <classes> <class name="TestNG.day2"> <methods> <include name="SecondTest"/> </methods> </class> </classes> </test> 
Enter fullscreen mode Exit fullscreen mode

you determine which URL is passed to which class with where it is put in the xml

in java-class
@Parameters("URL", "APIKey/usrname") @Test public void Demo(String urlname, String key){ System.out.println(urlname + key); } 
Enter fullscreen mode Exit fullscreen mode

with DataProvider Annotation

@Test(dataProvider = "getData") public void SecondTest(String username, String password){ System.out.println(username); System.out.println(password); } @DataProvider public Object[][] getData() { Object[][] data = new Object[3][2]; //1st set data[0][0] = "firstusername"; data[0][1] = "password"; //2nd set data[1][0] = "secondusername"; data[1][1] = "password"; //3rd set data[2][0] = "thirdusername"; data[2][1] = "password"; return data; } 
Enter fullscreen mode Exit fullscreen mode

ITestListener Interface

 import org.testng.ITestContext; import org.testng.ITestListener; import org.testng.ITestResult; public class Listeners implements ITestListener { @Override public void onTestSuccess(ITestResult result) { System.out.println("Successfully passed - as you can hopefully see."); } @Override public void onTestFailure(ITestResult result) { System.out.println("Successfully failed the test " + result.getName()); } } 
Enter fullscreen mode Exit fullscreen mode

add to XML file

<suite name="Suite"> <listeners> <listener class-name="TestNG.Listeners"/> </listeners> <parameter name="URL" value="qaclickacademy.com" /> 
Enter fullscreen mode Exit fullscreen mode

Run Tests parallel

e.g. for Performance Testing
in xml File

<suite name="Suite" parallel="tests" thread-count="2"> 
Enter fullscreen mode Exit fullscreen mode

which means 2 tests run at a time
for mobile testing you have to have multiply devices, for API fine
or <test parallel="classes" thread-count="2" >

Reporting

config-Default Report
then you'll find a testoutput Folder with a index.html, you can generate the report with
How to find the Report
Report in Browser

Top comments (0)