本文介绍了Selenium和TestNG使用'dependsOn'和'priority ='问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在GUI自动化测试中实现更好的工作流控制。我首先使用dependsOn开始,但很快发现缺点是,如果一个测试失败,整个套件的其余部分都不会运行。所以我切换到使用'priority =',但我看到了意想不到的行为。一个例子:

I am working on implementing better workflow control in my GUI automation tests. And I first started with dependsOn but quickly found the drawback is that if one test fails, the whole rest of the suite is not run. So I switched to using 'priority=' but am seeing unexpected behavior. One example:

@Test(priority = 10)
public void login(){...}

@Test(priority = 20, dependsOnMethods = "login")
public void verifyUserLogin() {...}

@Test(priority = 30, dependsOnMethods = "verifyUserLogin")
public void navigateToReportSettings() {...}

@Test(priority = 40, dependsOnMethods = "navigateToReportSettings")
public void verifyGeneralSettings() {...}

@Test(priority = 40, dependsOnMethods = "navigateToReportSettings")
public void verifyReportingPeriod() {...}
...
@Test(priority = 90, dependsOnMethods = "navigateToReportSettings")
public void saveReportSettings() {...}

我想要发生的事情:


  1. 登录。

  2. 验证用户是否已登录in。

  3. 导航到报告设置页面。

  4. 在报告设置页面上验证常规设置和报告周期(按任意顺序)

  5. 制作一些更改并保存。

  6. 重要提示:10,20和30必须成功或跳过其余部分。如果任何40次失败,则在所有40次完成后继续为50。但是不依赖任何步骤40s才能成功!

  1. Login.
  2. Verify user is logged in.
  3. Navigate to report settings page.
  4. Verify the general settings and reporting period on the report settings page (in any order)
  5. Make some changes and save.
  6. Important: 10, 20, and 30 must succeed or skip the rest. If any 40 fails continue to 50 after all 40 have completed. But without dependency on any step 40s to succeed!

发生了什么:


  1. 登录(优先级10)。

  2. 保存(优先级90)。

注意:还有群组注释,但我认为这与此无关。
提前感谢有关如何组合优先级和依赖于成功控制工作流程的任何提示,但依赖项仅在需要时使用。

Note: there are also 'groups' annotations but I don't think that is relevant here.Thanks in advance for any tips on how to combine priority and dependsOn successfully to control workflow, but with dependencies only used where needed.

这是另一个示例代码。
我不知道为什么它按此顺序运行:
输出:
10,20,30,40等... 110,// OK
130,140 ,150,160,//为什么跳过120个优先级?
120,120,120等... 120 //最后一次运行?
同样有趣的是,120s组可以按顺序重新编号(121,122,123等),但它们仍然是最后一次运行。

Here is another sample code.I have no idea why it is running in this order:Output: 10, 20, 30, 40, etc... 110, //OK130, 140, 150, 160, // Why were the 120 priorities skipped?120, 120, 120, etc... 120 //Run last?Also interesting is that the group of 120s can be renumbered sequentially (121, 122, 123, etc) and still they are run last.

因此问题必须是'dependsOn'和'priority ='根本不能很好地一起玩。而且我很好奇是否有人让这两个人在他们的环境中工作。谁知道它可能是一个但与Intellij IDEA?无论如何,我需要尽快到底,以避免以后进行昂贵的重构!再次感谢任何反馈 - JR

Therefore the issue must be that 'dependsOn' and 'priority =' simply do not play well together. And I'm curious if anyone has gotten these two working in their environment. Who knows maybe its a but with Intellij IDEA? Anyway I need to get to the bottom of this soon to avoid costly refactoring later! Thanks again for any feedback- JR

@Test(priority = 10, groups = "A")
public void login(){

System.out.println("10");

}


@Test(priority = 20, groups = {"A", "B"})
public void openUserAdministrationTest() {

    System.out.println("20");

}

@Test(priority = 30, groups = {"A", "B"})
public void usersTabTest() {

    System.out.println("30");

}

@Test(priority = 40, groups = {"A", "B"})
public void createUserTabTest() {

    System.out.println("40");

}


@Test(priority = 50, groups = {"A", "B"})
public void userCreationDataEntryTest() {

    System.out.println("50");

}

@Test(priority = 60, groups = {"A", "B", "C"})
public void userRolesTest() {

    System.out.println("60");

}

@Test(priority = 70, groups = {"A", "B"})
public void saveUserTest() {

    System.out.println("70");

}

@Test(priority = 80, groups = {"A", "B"})
public void closeUserAdminAndLogoutTest() {

    System.out.println("80");

}

@Test(priority = 90, groups = "A")
public void loginNavigateToUserAdmin() {

    System.out.println("90");
}

@Test(priority = 100, groups = {"A", "D"})
public void verifyUserSearchUserReturned() {

    System.out.println("100");

}

@Test(priority = 110, groups = {"A", "D"})
public void reOpenNewUserTest() {

    System.out.println("110");

}

@Test(priority = 120, groups = {"A", "E"}, dependsOnMethods = "reOpenNewUserTest")
public void verifyNewUserUserNameTest() {

    System.out.println("120");
}

@Test(priority = 120, groups = {"A", "E"}, dependsOnMethods = "reOpenNewUserTest")
public void verifyNewUserFullNameTest() {

    System.out.println("120");
}

@Test(priority = 120, groups = {"A", "E"}, dependsOnMethods = "reOpenNewUserTest")
public void verifyNewUserDepartmentTest() {

    System.out.println("120");
}

@Test(priority = 120, groups = {"A", "E"}, dependsOnMethods = "reOpenNewUserTest")
public void verifyNewUserPhoneNumberTest() {

    System.out.println("120");
}

@Test(priority = 120, groups = {"A", "E"}, dependsOnMethods = "reOpenNewUserTest")
public void verifyNewUserEmailTest() {

    System.out.println("120");
}

//      Note: password and active verified by user login

@Test(priority = 120, groups = {"A", "E"}, dependsOnMethods = "reOpenNewUserTest")
public void verifyNewUserActiveCheckedTest() {
    System.out.println("120");
}

@Test(priority = 120, groups = {"A", "E"}, dependsOnMethods = "reOpenNewUserTest")
public void verifyNewUserLanguageTest() {

    System.out.println("120");
}

@Test(priority = 120, groups = {"A", "E"}, dependsOnMethods = "reOpenNewUserTest")
public void verifyNewUserDateFormatTest() {

    System.out.println("120");
}

@Test(priority = 120, groups = {"A", "E"}, dependsOnMethods = "reOpenNewUserTest")
public void verifyNewUserNumberFormatTest() {

    System.out.println("120");
}

@Test(priority = 120, groups = {"A", "E"}, dependsOnMethods = "reOpenNewUserTest")
public void verifyNewUserReportingPeriodTest() {

    System.out.println("120");
}

@Test(priority = 120, groups = {"A", "E"}, dependsOnMethods = "reOpenNewUserTest")
public void verifyNewUserReportingPeriodExampleTest() {

    System.out.println("120");
}

@Test(priority = 120, groups = {"A", "E"}, dependsOnMethods = "reOpenNewUserTest")
public void verifyNewUserReferencePeriodTest() {

    System.out.println("120");
}

@Test(priority = 120, groups = {"A", "E"}, dependsOnMethods = "reOpenNewUserTest")
public void verifyNewUserReferencePeriodExampleTest() {

    System.out.println("120");
}

@Test(priority = 120, groups = {"A", "E"}, dependsOnMethods = "reOpenNewUserTest")
public void verifyNewUserShowAnnotationsCheckedTest() {
    System.out.println("120");
}

@Test(priority = 130, groups = {"A", "C"})
public void verifyNewUserRoleTest() {

    System.out.println("130");
}


@Test(priority = 140, groups = {"A", "C"})
public void verifyNewUserFunctionalRoleTest() {

    System.out.println("140");

}

@Test(priority = 150, groups = {"A", "C"})
public void verifyUserAdminCloseAndLogoutTest() {

    System.out.println("150");

}

@Test(priority = 160, groups = {"A", "C"})
public void verifyUserLogin() {

    System.out.println("160");

}

这是一个更简单的例子,但也显示了如何依赖简单地打破优先顺序:

This is a much simpler example, but also showing how depends on simply breaks priorities:

@Test(priority = 10)
public void test10(){
    System.out.println("10");
}

@Test(priority = 20, dependsOnMethods = "test10")
public void test20() {
    System.out.println("20, depends on 10");
}

@Test(priority = 30, dependsOnMethods = "test20")
public void test30() {
    System.out.println("30, depends on 20");
}

@Test(priority = 40, dependsOnMethods = "test10")
public void test40() {
    System.out.println("40, depends on 10");
}

应该运行:10,20,30,40.
运行:10,20,40,30。

Should run: 10, 20, 30, 40.Runs: 10, 20, 40, 30.

推荐答案

不提供优先权并依赖于一起,你可以分组测试。您可以这样做
例如,

@Test(priority = 10, groups = { "10" })
public void test10() {
    System.out.println("10");
}

@Test(dependsOnMethods = "test10", groups = { "10" })
public void test20() {
    System.out.println("20, depends on 10");
}

@Test(dependsOnGroups = { "10" })
public void test30() {
    System.out.println("30, depends on 20");
}

@Test(dependsOnMethods = "test30")
public void test40() {
    System.out.println("40, depends on 10");
}

必须运行的第二件事(成功或跳过其余部分)

Second thing for must run (succeed or skip the rest)

您将始终按照您所依赖的方法运行,即使其中一些方法失败了。当您只是想确保您的测试方法按特定顺序运行时,这很有用,但它们的成功并不真正取决于其他人的成功。通过在@Test注释中添加alwaysRun = true来获得软依赖。

You will always be run after the methods you depend on, even if some of them have failed. This is useful when you just want to make sure that your test methods are run in a certain order but their success doesn't really depend on the success of others. A soft dependency is obtained by adding "alwaysRun=true" in your @Test annotation.

如果依赖的方法失败并且您对它有一个硬依赖(alwaysRun = false,这是默认值),依赖它的方法不标记为FAIL而是标记为SKIP。跳过的方法将在最终报告中报告(在HTML中颜色既不是红色也不是绿色),这很重要,因为跳过的方法不一定是失败的。

If a method depended upon fails and you have a hard dependency on it (alwaysRun=false, which is the default), the methods that depend on it are not marked as FAIL but as SKIP. Skipped methods will be reported as such in the final report (in a color that is neither red nor green in HTML), which is important since skipped methods are not necessarily failures.

这篇关于Selenium和TestNG使用'dependsOn'和'priority ='问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 10:00