开发者

TestNg, annotation "beforeTestMethod" and override

开发者 https://www.devze.com 2022-12-08 03:57 出处:网络
For my tests I\'m using a base class MyTestBase defining a method setup() that does some base preparations:

For my tests I'm using a base class MyTestBase defining a method setup() that does some base preparations:

public class MyTestBase {
开发者_运维技巧    @Configuration( beforeTestMethod=true )
    protected void setup() {
        // do base preparations
    }
}

Now I have some more specific test classes that have to do their own preparations. There are different ways how to implement this.

I could use @Override:

public class MySpecialTestBase extends MyTestBase {
    @Override
    protected void setup() {
        super.setup();
        // do additional preparations
    }
}

...or I could use a separate setup method:

public class MySpecialTestBase extends MyTestBase {
    @Configuration( beforeTestMethod=true )
    protected void setupSpecial() {
        // do additional preparations
    }
}

Is there a prefered way to implement this?


I would prefer using @Configuration annotation. @Override and super are more fragile. You can forget to call super.setup(), or call it in wrong place. Meanwhile, using separate method with @Configuration lets you to choose more appropriate naming for child setup method if necessary, and you get setup order guaranteed by TestNG (parent then child).

Two more points:

  1. I'd make parent setup final in order to forbid accidental overriding.
  2. I'd use @BeforeMethod annotations. They are available since TestNG 5.0. Of course, for older versions you forced to using @Configuration.
0

精彩评论

暂无评论...
验证码 换一张
取 消