不同测试类中使用的通用注解

Kam*_*mil 3 java testng annotations testng-annotation-test

@BeforeTest我有一个关于or之类的注释的问题@BeforeMethod。是否可以设置全局注释,以便我的所有测试类都将使用它们?我的框架中有 20 多个测试类,其中有很多测试方法。每个测试类都有类似@BeforeTest或 的先决条件@BeforeMethod,但对于每个测试类,此先决条件是相同的。所以我认为这可能是一个好主意,编写一个通用的注释方法,可以在每个测试类中使用。

kat*_*ris 5

使用继承使代码可重用。创建一个超级类DefaultTestCase

public class DefaultTestCase{
  @BeforeTest
  public void beforeTest() {
     System.out.println("beforeTest");
  }  
  @BeforeMethod
  public void beforeMethod() {
    System.out.println("beforeMethod");
  }  
}
Run Code Online (Sandbox Code Playgroud)

每个测试用例类都扩展DefaultTestCase

public class ATest extends DefaultTestCase{
  @Test
  public void test() {
     System.out.println("test");
  }
  @Test
  public void anotherTest() {
     System.out.println("anotherTest");
  }
}
Run Code Online (Sandbox Code Playgroud)

输出:

beforeTest
beforeMethod
test
beforeMethod
anotherTest
Run Code Online (Sandbox Code Playgroud)