testng beforemethod的firsttimeonly可选元素的用法是什么?如果为true并且将要运行的@test方法的invocationcount>1,则此beforethod将只被调用一次(在第一次测试调用之前)。使用@beforetest似乎是多余的,因为它更清楚@beforetest:在运行属于标记内类的任何测试方法之前,将运行带注解的方法。
vdzxcuhz1#
@BeforeMethod 在每次测试之前调用,而 @BeforeTest 在执行任何测试之前调用一次。所以,有一个 firstTimeOnly 上的属性 @BeforeTest ,根据设计,每个测试组只执行一次。另一方面,自从 @BeforeMethod 可能执行多次(如果使用 invocationCount ),可以告诉testng只执行带注解的方法 BeforeMethod 一次。这个答案有一个很好的例子来说明这两个注解之间的区别。让我们用一个例子来说明这种行为:
@BeforeMethod
@BeforeTest
firstTimeOnly
invocationCount
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()
beforeMethod
methodOne()
@BeforeMethod(firstTimeOnly = true)
试验前before方法执行方法一执行方法一before方法执行方法一现在, beforeMethod() 以前只执行一次 methodOnd() ,即使测试执行了两次。
methodOnd()
1条答案
按热度按时间vdzxcuhz1#
@BeforeMethod
在每次测试之前调用,而@BeforeTest
在执行任何测试之前调用一次。所以,有一个
firstTimeOnly
上的属性@BeforeTest
,根据设计,每个测试组只执行一次。另一方面,自从
@BeforeMethod
可能执行多次(如果使用invocationCount
),可以告诉testng只执行带注解的方法BeforeMethod
一次。这个答案有一个很好的例子来说明这两个注解之间的区别。
让我们用一个例子来说明这种行为:
这张照片:
试验前
before方法
执行方法一
before方法
执行方法一
before方法
执行方法一
注意
beforeMethod()
在每个方法之前执行,而beforeMethod
在执行任何测试之前执行一次。还要注意beforeMethod
在每次执行之前执行methodOne()
.现在,让我们添加
firstTimeOnly
属性:试验前
before方法
执行方法一
执行方法一
before方法
执行方法一
现在,
beforeMethod()
以前只执行一次methodOnd()
,即使测试执行了两次。