- Install TestNG
- Basic Test-Annotation
- Create an XML
- Refere to packages
- Before/ After - Annotations
- run specific Tests with
- with a little help of attributes
- Parametrizing
- ITestListener Interface
- Run Tests parallel
- 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(){.... }
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>
Refere to packages
<?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>
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"); } }
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>
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>
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); }
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; }
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()); } }
add to XML file
<suite name="Suite"> <listeners> <listener class-name="TestNG.Listeners"/> </listeners> <parameter name="URL" value="qaclickacademy.com" />
Run Tests parallel
e.g. for Performance Testing
in xml File
<suite name="Suite" parallel="tests" thread-count="2">
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
then you'll find a testoutput Folder with a index.html, you can generate the report with
Top comments (0)