3

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

@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. 验证用户是否已登录。
  3. 导航到报告设置页面。
  4. 验证报告设置页面上的常规设置和报告周期(以任意顺序)
  5. 进行一些更改并保存。
  6. 重要提示:10、20 和 30 必须成功或跳过其余部分。如果任何 40 个失败,则在所有 40 个都完成后继续到 50 个。但不依赖任何步骤 40s 成功!

怎么了:

  1. 登录(优先级 10)。
  2. 保存(优先级 90)。

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

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

因此问题一定是'dependsOn'和'priority ='根本不能很好地结合在一起。我很好奇是否有人让这两个在他们的环境中工作。谁知道它可能是 Intellij IDEA?无论如何,我需要尽快了解这一点,以避免以后进行昂贵的重构!再次感谢您的任何反馈 - 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");

}

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

@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。

4

3 回答 3

2

不提供优先级和依赖在一起,可以将测试分组。你可以这样做 例如,

@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");
}

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

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

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

于 2016-10-27T07:10:01.507 回答
1

有很多与dependsOnMethods 注解相关的缺陷。我在 6.5.2 中遇到过类似的情况。尝试将您的依赖项更新到 6.8.3。

于 2013-09-05T10:53:15.550 回答
1

要使测试即使在失败后也能运行,请使用alwaysRun属性和dependsOnMethods而不是使用priority属性,alwaysRun属性让下一个依赖项即使在失败后也能执行,请尝试以下语法:

@Test
public void login(){...}

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

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

@Test(dependsOnMethods = "navigateToReportSettings", alwaysRun=true)
public void verifyGeneralSettings() {...}

@Test(dependsOnMethods = "navigateToReportSettings", alwaysRun=true)
public void verifyReportingPeriod() {...}
...
@Test(dependsOnMethods = "navigateToReportSettings")
public void saveReportSettings() {...}
于 2017-10-04T09:07:02.030 回答