未遵循 testNG 优先级

testNG priorities not followed

在 testNG.xml 文件中,我有 10 多个测试 classes(在测试套件标签内)用于回归测试。然后,我通过在 @Test 注释中使用 priority=xxx,以特定顺序对几个测试 classes 进行了自动化测试。特定 class 中的优先级值是连续的 - 但每个测试 class 具有不同的范围。例如:

testClass1 : values are from 1-10 
testClass2 : values are from 11-23    
testClass3 : values are from 31-38 
. 
. 
. 
lastTestClass : values are from 10201-10215

这样做的目的是要有一个特定的顺序来执行 10 多个测试-classes。我需要在测试执行结束时执行一个测试-class - 因此,class 中的优先级范围为 10201-10215。但是,此特定测试-class 在第一个 class 之后立即进行测试,优先级为 1-10.

按特定顺序运行 设计测试是一种不好的做法。您可能希望将来 运行 并行测试 - 对顺序的依赖会阻止您这样做。

考虑改用 TestNG 侦听器:

看起来您正在尝试在测试后实施某种拆卸过程。 如果是这种情况 - 您可以实施 ITestListener 并在执行所有测试后使用 onFinish 方法 运行 一些代码。

此外,此 TestNG 注释可能适用于您的情况:

org.testng.annotations.AfterSuite

我建议您使用依赖关系,而不是使用优先级。他们将 运行 您的测试严格按照顺序进行,永远不会 运行 在依赖之前先测试依赖,即使您是并行 运行 测试也是如此。

我知道你在不同的 类 中有不同的范围,所以在 dependOnMethods 中你必须指定你引用的测试的根:

@Test(  description = "Values are from 1-10")
public void values_1_10() {
    someTest();
}

@Test(  description = "Values are from 21-23",
        dependsOnMethods = { "com.project.test.RangeToTen.values_1_10" })
public void values_21_23() {
    someTest();
}


如果您在每个范围内有多个测试,那么您可以使用 dependsOnGroups:

@Test(  enabled = true,
        description = "Values are from 1-10")
public void values_1_10_A() {
    someTest();
}

@Test(  enabled = true,
        description = "Values are from 1-10")
public void values_1_10_B() {
    someTest();
}

@Test(  enabled = true,
        description = "Values are from 1-10",
        dependsOnGroups = { "group_1_10" })
public void values_21_23_A() {
    someTest();
}

@Test(  enabled = true,
        description = "Values are from 1-10",
        dependsOnGroups = { "group_1_10" })
public void values_21_23_B() {
    someTest();
}


您也可以使用 testng.xml 中的更多选项执行相同操作: https://testng.org/doc/documentation-main.html#dependencies-in-xml

您的另一个选择是使用 "preserve order": https://www.seleniumeasy.com/testng-tutorials/preserve-order-in-testng

但是正如 Anton 提到的那样,如果您想要并行 运行,那可能会给您带来麻烦,所以我建议您使用依赖项。