Reputation: 53
I am writing TestNG automation code for my project . In this I should Login in an account before the test starts (This applies for all test classes) . Is this possible that I create a class "BaseClass" that has only @BeforeTest annotated method . Does my test classes identifies the base class .Or can I follow any other methods . The reason I am going for a base class is that I dont like to repeat the @BeforeTest method in each test classes
Upvotes: 1
Views: 350
Reputation: 53
Yes, This is possible. We can have a BaseTestClass which is extended by all test classes and use @BeforeTest method in BaseTestClass to run before every test in every class that extends this.
public class BaseTestClass {
@BeforeClass
public void initializeTestClass() {
System.out.println("Before class");
}
}
public class TestClassA extends BaseTestClass {
@Test
public void testA() {
System.out.println("Test A");
}
}
public class TestClassB extends BaseTestClass {
@Test
public void testB() {
System.out.println("Test B");
}
}
In this above code, Output while running a suite that contains testClassA and TestClassB be like
Before Class
Test A
Before Class
Test B
Although as far as I have seen, This is a bad practice as when the testng scope becomes larger, The code becomes messier.
Upvotes: 0