TestNG Notes


Frequently used cribs...

Imports

TestNG imports TestNG static imports
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import static org.testng.Assert.fail;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertNotSame;

@BeforeMethod behavior

@BeforeMethod is invoked before each method with a @Test annotation.

@BeforeTest is invoked before a <test> tag in the testng.xml file:

<test>
  <classes>
    <class name="A" />  ← contains tests a1() and a2()
    <class name="B" />  ← contains tests b1() and b2()
  </classes>
</test>
In this case, you will see:
@BeforeTest
  @BeforeMethod
    a1()
  @AfterMethod
  @BeforeMethod
    a2()
  @AfterMethod
  @BeforeMethod
    b1()
  @AfterMethod
  @BeforeMethod
    b22()
  @AfterMethod
@AfterTest