TestNg, annotation "beforeTestMethod" and override
For my tests, I use a base class MyTestBase
that defines a method setup()
that executes some basic commands:
public class MyTestBase {
@Configuration( beforeTestMethod=true )
protected void setup() {
// do base preparations
}
}
Now I have a few more special test classes to do on their own. There are various ways to accomplish this.
I could use @Override
:
public class MySpecialTestBase extends MyTestBase {
@Override
protected void setup() {
super.setup();
// do additional preparations
}
}
... or I can use a separate installation method:
public class MySpecialTestBase extends MyTestBase {
@Configuration( beforeTestMethod=true )
protected void setupSpecial() {
// do additional preparations
}
}
Is there a preferred way to implement this?
source to share
I would rather use annotation @Configuration
. @Override
and super
more fragile. You may forget to call super.setup()
or call him in the wrong place. In the meantime, using a separate c method @Configuration
, you can choose a more appropriate name for the child install method if needed, and you get an install order guaranteed by TestNG (parent then child).
Two more points:
- I would set a parent setting
final
to prevent accidental overrides. - I would use annotations
@BeforeMethod
. They are available since TestNG 5.0. Of course, for older versions, you are forced to use@Configuration
.
source to share