Skip to content

Instantly share code, notes, and snippets.

@gwpantazes
Last active February 15, 2018 17:23
Show Gist options
  • Save gwpantazes/ad4f03457616df720160ac74edcc0594 to your computer and use it in GitHub Desktop.
Save gwpantazes/ad4f03457616df720160ac74edcc0594 to your computer and use it in GitHub Desktop.
TestNG Method Precedence

TestNG Annotation Precedence

This is an exploration of the precedence order for TestNG annotations so I can get a better understanding of which annotations come first.

As we can see in testOutput.log, there is a clear order to these methods (I wish they put that order more clearly in the TestNG Documentation...

The original question was "What is the difference between @BeforeMethod and @BeforeTest?" After seeing the results, it's clear that @BeforeMethod refers to each individual @Test annotated method, while @Test refers to the <test> organizational blocks in the testng.xml test suite file.

One thing that confused me is that @BeforeGroups and @AfterGroups both needed to be supplied with their groups, when I originally thought they would automatically run before all groups. It does make sense though, once I found out what was going on.

Another interesting thing is I think I found an IntelliJ TestNG integration bug where running a Groups test fails because the parallel attribute for the test suite isn't set.

import org.testng.annotations.*;
class TestNGPrecedence {
@BeforeSuite
public void beforeSuite() {
System.out.println("Before Suite");
}
@AfterSuite
public void afterSuite() {
System.out.println("After Suite");
}
@BeforeTest
public void beforeTest() {
System.out.println("Before Test");
}
@AfterTest
public void afterTest() {
System.out.println("After Test");
}
@BeforeGroups(groups = { "grouptest" })
public void beforeGroups() {
System.out.println("Before Groups");
}
@AfterGroups(groups = { "grouptest" })
public void afterGroups() {
System.out.println("After Groups");
}
@BeforeClass
public void beforeClass() {
System.out.println("Before Class");
}
@AfterClass
public void afterClass() {
System.out.println("After Class");
}
@BeforeMethod
public void beforeMethod() {
System.out.println("Before Method");
}
@AfterMethod
public void afterMethod() {
System.out.println("After Method");
}
@Test
public void test1() {
System.out.println("\nTest1: This is a test.\n");
}
@Test(groups = {"grouptest"})
public void test2() {
System.out.println("\nTest2: This is another test (with group \"grouptest\").\n");
}
@Test
public void test3() {
System.out.println("\nTest3: This is the last test.\n");
}
}
Before Suite
Before Test
Before Class
Before Method
Test1: This is a test.
After Method
Before Groups
Before Method
Test2: This is another test (with group "grouptest").
After Method
After Groups
Before Method
Test3: This is the last test.
After Method
After Class
After Test
After Suite
===============================================
Default Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment