指定dependsOnMethods时,testng未按优先级顺序运行

每当我们在@Test注释方法上指定prioritydependsOnMethods时,测试方法的执行顺序不是根据优先级。 为什么会这样? 以下是演示该问题的测试类:

 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。 为什么t4在t1之后执行,当t2和t3的优先级高于t4?

TIA

将首先执行所有独立方法(没有@dependsOnMethods依赖关系)。 然后将执行具有依赖性的方法。 如果即使在此排序之后执行顺序存在歧义,优先级也会出现。

这是订购方案:

  1. 执行所有独立方法(没有@dependsOnMethods注释的方法)
  2. 如果此排序存在歧义,则使用优先级来解决独立方法的歧义
  3. 按依赖顺序执行依赖方法
  4. 如果此排序存在歧义,则使用优先级来解决依赖方法的歧义
  5. 如果仍然存在歧义(由于不使用优先级或两个具有相同优先级的方法),则根据字母顺序对它们进行排序。

现在所有的歧义都得到了解决,因为没有两个方法可以具有相同的名称。

我今天遇到了同样的问题。

起初,我只对我的测试使用priority ,但后来我还需要添加dependsOnMethods

最初我只将dependsOnMethods添加到我的一些@Test方法中。 结果,我的测试的执行顺序已经乱成一团。

我已经阅读了很多与这个主题相关的文章和讨论,事实certificate,使用prioritydependsOnMethods属性togeter会给整个图片带来很多不确定性,并且TestNG的行为将永远无法预测并且在这个情况。

我的解决方案是将dependsOnMethods添加到我的所有测试方法中,同时我也保留了所有 mehtods的priority 。 现在他们的执行顺序恢复正常,同时我从dependsOnMethods的function中受益。 即链中的第一个失败的测试方法导致所有后续测试方法被跳过并在报告中显示正确。

这是我的测试类的片段:

  @Test(priority = 2, dependsOnMethods= {"Meganav_Point_C1_and_Click_C3"}) public void Click_product_in_Category_result_page() throws Throwable { Grid.clickProduct(1, 1); } @Test(priority = 3, dependsOnMethods= {"Click_product_in_Category_result_page"}) public void PDP_setQty() throws Throwable { ProductDetailsPage.setQty(2); } @Test(priority = 4, dependsOnMethods= {"PDP_setQty"}, alwaysRun= true) public void PDP_click_Add_To_Basket() throws Throwable { ProductDetailsPage.addToBasket(); } 

希望这可以帮助。

此致,Veselin Petrov