TestNG Annotations

What we have learned so far?

Part 1 – What Is Testng? How To Configure Testng In Eclipse?
https://www.onlyfullstack.com/what-is-testng-how-to-configure-testng-in-eclipse/

TestNG Annotations

Here is a quick overview of the annotations available in TestNG
@BeforeSuite: The annotated method will be run before all tests in this suite.
@AfterSuite: The annotated method will be run after all tests in this suite.
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run.
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the 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.
@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.

Lets write our first test class –

package com.onlyfullstack.service;

import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class FirstTest {

    @BeforeClass
    public void beforeClass() {
        System.out.println("@BeforeClass");
    }

    @AfterClass
    public void afterClass() {
        System.out.println("@AfterClass");
    }

    @BeforeMethod
    public void beforeMethod() {
        System.out.println("@BeforeMethod");
    }

    @AfterMethod
    public void afterMethod() {
        System.out.println("@AfterMethod");
    }

    @Test
    public void runTest1() {
        System.out.println("@Test - runTest1");
    }

    @Test
    public void runTest2() {
        System.out.println("@Test - runTest2");
    }
    @Test
    public void firstTest() {
        String sample = "Employee";
        Assert.assertEquals("EMPLOYEE", sample.toUpperCase());
        System.out.println("@Test - firstTest");
    }
}

Lets run our test case –

In Eclipse you can run a test case as Run As -> TestNG Test

onlyfullstack testng testng running test case
In IntelliJ you can run the test case as below –
onlyfullstack testng running test case intellij
In Maven you can run mvn clean test to run the test cases
Output –
@BeforeClass

@BeforeMethod

@Test - firstTest

@AfterMethod

@BeforeMethod

@Test - runTest1

@AfterMethod

@BeforeMethod

@Test - runTest2

@AfterMethod

@AfterClass

===============================================
Default Suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0
Lets go to our next tutorial where we will discuss below points :

Part 3 – How To Run Multiple Test Suites In TestNG With Surefire Plugin?

– What is TestNG Suite?
– How to write testng-suite.xml
– How to configure the SureFire Plugin with TestNG?
https://www.onlyfullstack.com/how-to-run-test-suite-in-testng-with-surefire-plugin/

Source Code
You can find the source code on below link – 
https://github.com/onlyfullstack/testng-tutorial

TestNG Tutorial
https://www.onlyfullstack.com/testng-tutorial-for-beginners/