testng beforemethod的firsttimeonly用法

3pmvbmvn  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(598)

testng beforemethod的firsttimeonly可选元素的用法是什么?
如果为true并且将要运行的@test方法的invocationcount>1,则此beforethod将只被调用一次(在第一次测试调用之前)。
使用@beforetest似乎是多余的,因为它更清楚
@beforetest:在运行属于标记内类的任何测试方法之前,将运行带注解的方法。

vdzxcuhz

vdzxcuhz1#

@BeforeMethod 在每次测试之前调用,而 @BeforeTest 在执行任何测试之前调用一次。
所以,有一个 firstTimeOnly 上的属性 @BeforeTest ,根据设计,每个测试组只执行一次。
另一方面,自从 @BeforeMethod 可能执行多次(如果使用 invocationCount ),可以告诉testng只执行带注解的方法 BeforeMethod 一次。
这个答案有一个很好的例子来说明这两个注解之间的区别。
让我们用一个例子来说明这种行为:

public class TestClass {

  @Test(invocationCount = 2)
  public void methodOne() {
    System.out.println("executing method one");
  }

  @Test
  public void methodTwo() {
    System.out.println("executing method one");
  }

  @BeforeMethod
  public void beforeMethod() {
    System.out.println("before method");
  }

  @BeforeTest
  public void beforeTest() {
    System.out.println("before test");
  }

}

这张照片:
试验前
before方法
执行方法一
before方法
执行方法一
before方法
执行方法一
注意 beforeMethod() 在每个方法之前执行,而 beforeMethod 在执行任何测试之前执行一次。还要注意 beforeMethod 在每次执行之前执行 methodOne() .
现在,让我们添加 firstTimeOnly 属性:

@BeforeMethod(firstTimeOnly = true)

试验前
before方法
执行方法一
执行方法一
before方法
执行方法一
现在, beforeMethod() 以前只执行一次 methodOnd() ,即使测试执行了两次。

相关问题