Skip to content
Advertisement

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

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?

Advertisement

Answer

Use @BeforeMethod, not @BeforeTest.

The meaning of @BeforeTest is explained in the documentation.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement