TestNg's @BeforeTest on base class only happening once per fixture

ripper234 picture ripper234 · Nov 30, 2010 · Viewed 38.8k times · Source

I'm trying to use @BeforeTest to get code to ... run once before every test.

This is my code:

public class TestBase {
    @BeforeTest
    public void before() {
        System.out.println("BeforeTest");
    }
}

public class TestClass extends TestBase{
    @Test
    public void test1(){}

    @Test
    public void test2(){}
}

"BeforeTest" is only printed once, not twice. What am I doing wrong?

Answer

Cedric Beust picture Cedric Beust · Nov 30, 2010

Use @BeforeMethod, not @BeforeTest.

The meaning of @BeforeTest is explained in the documentation.