Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of Maven+Surefire+Testng - BeforeClass, BeforeTest, Test, Test, AfterTest, AfterClass

I expected the following output:

Running TestSuite
[DEBUG] beforeClass
[DEBUG] beforeTest
[DEBUG] test
[DEBUG] afterTest
[DEBUG] beforeTest
[DEBUG] test
[DEBUG] afterTest
[DEBUG] afterClass

But instead, this actually happens. Notice 2 problems: BeforeClass runs after BeforeTest. Second, Before/AfterTest runs once.

Running TestSuite
[DEBUG] beforeTest
[DEBUG] beforeClass
[DEBUG] test
[DEBUG] test
[DEBUG] afterClass
[DEBUG] afterTest

Here's the code.

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TestNgAnnoTest {
   @BeforeClass
   public void beforeClass(){
      System.out.println("beforeClass");
   }
   @BeforeTest
   public void beforeTest(){
      System.out.println("beforeTest");
   }
   @Test
   public void test1(){
      System.out.println("test");
   }
   @Test
   public void test2(){
      System.out.println("test");
   }
   @AfterTest
   public void afterTest(){
      System.out.println("afterTest");

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

For anyone curious about tool versions: java macosx/1.6.0_22, mvn: 2.2.1, surefire 2.6, testng 5.14.2

like image 221
campbell Avatar asked Jan 22 '23 00:01

campbell


1 Answers

Stupid me. Confused After/BeforeTest with After/BeforeMethod.

[DEBUG] beforeClass
[DEBUG] beforeMethod
[DEBUG] test
[DEBUG] afterMethod
[DEBUG] beforeMethod
[DEBUG] test
[DEBUG] afterMethod
[DEBUG] afterClass

Replacing After/BeforeTest produced the correct exec ordering.

    @BeforeClass
    public static void beforeClass(){
        log.debug("beforeClass");
    }

    @BeforeMethod
    public void beforeMethod(){
        log.debug("beforeMethod");
    }

    @Test
    public void test1(){
        log.debug("test");
    }

    @Test
    public void test2(){
        log.debug("test");
    }

    @AfterMethod
    public void afterMethod(){
        log.debug("afterMethod");

    }

    @AfterClass
    public void afterClass(){
        log.debug("afterClass");
    }
like image 70
campbell Avatar answered Apr 07 '23 16:04

campbell