Getting started with TestNG Java Framework
Published: 2021-02-12 11:45:20

1. [Install TestNG](#install-testng)
2. [Basic Test-Annotation](#basic-testannotation)
3. [Create an XML](#create-an-xml)
4. [Refere to packages](#refere-to-packages)
5. [Before/ After - Annotations](#before-after-annotations)
     1. [xml-Level](#xmllevel)
     2. [Class-Level](#classlevel)
6. [run specific Tests with](#run-specific-tests-with-raw-groups-endraw-)
7. [with a little help of attributes](#with-a-little-help-of-attributes)
     3. [Order Methods](#order-methods)
     4. [Skip Test](#skip-test)
     5. [TimeOut](#timeout)
8. [Parametrizing](#parametrizing)
     6. [in XML file (global environment variables)](#in-xml-file-global-environment-variables)
         1. [in xml](#in-xml)
         2. [in java-class](#in-javaclass)
     7. [with DataProvider Annotation](#with-dataprovider-annotation)
9. [ITestListener Interface](#itestlistener-interface)
10. [Run Tests parallel](#run-tests-parallel)
11. [Reporting](#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: 
```Java
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!) 
```java
<?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 
![Image of Package in Code](https://i.imgur.com/eAiPCGh.png)
e.g. for Regressiontests
```java
<?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`

```java
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

```java
<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 
```java
<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
```java
@Parameters("URL", "APIKey/usrname")
@Test
    public void Demo(String urlname, String key){ System.out.println(urlname + key); }
```
#### with DataProvider Annotation

```java
@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

```java


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 
```java
<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 
```java 
<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 
![config-Default Report](https://i.imgur.com/G3QpSU4.png)
then you'll find a testoutput Folder with a index.html, you can generate the report with 
![How to find the Report](https://i.imgur.com/3ej8JRG.png)
![Report in Browser](https://i.imgur.com/HcgB9MA.png)