问题描述
每当我们在 @Test
注释方法上指定 priority
和 dependsOnMethods
时,测试方法的执行顺序不是根据优先级.为什么会这样?这是演示问题的测试类:
Whenever we specify priority
and dependsOnMethods
on a @Test
annotated method, the order of execution of test methods is not according to the priority. why is so?Here is the test class to demonstrate the issue:
package unitTest.TestNGTestCases;
import org.testng.annotations.Test;
public class TestNGTest1 {
@Test(priority=1)
public void t1()
{
System.out.println("Running 1");
}
@Test(priority=2,dependsOnMethods="t1")
public void t2()
{
System.out.println("Running 2");
}
@Test(priority=3,dependsOnMethods="t2")
public void t3()
{
System.out.println("Running 3");
}
@Test(priority=4)
public void t4()
{
System.out.println("Running 4");
}
}
实际输出:
Running 1
Running 4
Running 2
Running 3
===============================================
All Tests Suite
Total tests run: 4, Failures: 0, Skips: 0
===============================================
预期输出:
Running 1
Running 2
Running 3
Running 4
===============================================
All Tests Suite
Total tests run: 4, Failures: 0, Skips: 0
===============================================
测试执行的顺序应该是 t1、t2、t3、t4.当 t2 和 t3 的优先级高于 t4 时,为什么 t4 在 t1 之后执行?
The order of test execution should have been t1, t2, t3, t4. why is t4 getting executed after t1, when t2 and t3 have higher priority then t4?
TIA
推荐答案
所有独立的方法(没有@dependsOnMethods 依赖)将首先执行.然后将执行具有依赖关系的方法.如果在此排序之后执行顺序仍存在歧义,则优先考虑.
All independent methods (that do not have @dependsOnMethods dependency) will be executed first. Then methods with dependency will be executed. If there is ambiguity in execution order even after this ordering, priority comes into picture.
这是订购方案:
- 执行所有独立的方法(没有@dependsOnMethods注解的方法)
- 如果这个排序有歧义,使用优先级来解决独立方法的歧义
- 按依赖顺序执行依赖方法
- 如果这个排序有歧义,使用优先级来解决依赖方法的歧义
- 如果仍然存在歧义(由于没有使用优先级或两个具有相同优先级的方法),请根据字母顺序对其进行排序.
现在所有的歧义都解决了,因为没有两个方法可以具有相同的名称.
Now all ambiguity is resolved since no two methods can have the same name.
这篇关于指定了dependsOnMethods 时,testng 未按优先级顺序运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!