我在testNG的优先级和依赖性方面遇到了一些问题。例如,如果我有这个
@Test
public void login () { ... }
@Test (dependsOnMethods = {"login"})
public void method1 () { ... }
@Test (dependsOnMethods = {"method1"})
public void method2 () { ... }
@Test (dependsOnMethods = {"login"})
public void logout () { ... }
在这种情况下,它运行如下:
login - > method1 - > logout - > method2
这不起作用,因为我不再连接了
你要对我说,退出取决于方法2,一切都会有效......
是的它会...但是当method1或2失败时,它会跳过注销而不是这样做......这不是我想要的。
然后你会说我......在这种情况下,只需运行优先级而不是依赖性....是的但是如果method1失败...那么method2可能会很好但是因为method1失败所以不会工作所以我会有假阴性。
关于如何做到这一点的任何想法?
你应该使用之前/之后的方法来做到这一点:
@BeforeClass
public void login () { ... }
@Test
public void method1 () { ... }
@Test(dependsOnMethods = {"method1"})
public void method2 () { ... }
@AfterClass(alwaysRun=true) // alwaysRun to run the after method even if a test fails
public void logout () { ... }
你可以声明logout
依赖于“method2”和always run:
alwaysRun
public abstract boolean alwaysRun
如果设置为true,则即使依赖于失败的方法,也始终会运行此测试方法。如果此测试不依赖于任何方法或组,则将忽略此属性。
默认:
false
例:
@Test
public void login () { ... }
@Test (dependsOnMethods = {"login"})
public void method1 () { ... }
@Test (dependsOnMethods = {"method1"})
public void method2 () { ... }
@Test (dependsOnMethods = {"method2"}, alwaysRun = true)
public void logout () { ... }
您可以将方法定义为:
@Test(priority=1)
public void login()
{
System.out.println("login");
}
@Test(priority=2)
public void method1()
{
System.out.println("Method1");
Assert.assertEquals("Method1", "login", "invalid msg not verified"); // Here test is failing as the expected value is not matching the actual value
System.out.println("Verified");
}
@Test(priority=3)
public void method2()
{
System.out.println("Method2");
Assert.assertEquals("Method2", "Method2", "invalid msg not verified"); // Method1 fails however method2 is executed as actual and expected value are matching.
System.out.println("Verified");
}
@Test(priority=4)
public void logout()
{
System.out.println("logout");
}
这将帮助您执行所有优先级为1的测试,然后只有优先级为2,然后是3,然后是4.我在上面的代码中添加了“Assert”,这将有助于您进一步推进如果任何测试失败。
我相信当问题很好地集思广益的同时,在实施过程中很少有概念被忽略。希望这提供了解决问题的方法:
@Test
public void login () { ... }
@Test (dependsOnMethods = {"login"}, groups = { "afterLogin" })
public void method1 () { ... }
@Test (dependsOnMethods = {"method1"}, groups = { "afterLogin" }, priority = 1)
public void method2 () { ... }
// you mark dependsOnMethods when it actually "depends on" something prior to its execution and
// hence corresponds to the failure of method it is depending on
@Test (dependsOnMethods = {"login"}, priority = 2)
public void logout () { ... }
这将确保序列为login - > method1 - > method2 - > logout
哪里,
案例1:如果login
失败,则不应执行依赖于它的方法。
情况2:如果方法1或2失败,它将不会跳过logout
测试
情况3:如果方法1和方法2不依赖于登录,您实际上可以执行优先级更低/更高/相等的方法来确保they are all independent
。
可能这可以帮助
@Test (priority=1)
public void login()
{
System.out.println("login");
}
@Test(priority=2,dependsOnMethods={"login"})
public void method1()
{
System.out.println("method1");
}
@Test(priority=3,dependsOnMethods={"method1"})
public void method2()
{
System.out.println("method2");
}
@Test(priority=4,dependsOnMethods={"login"})
public void logout()
{
System.out.println("logout");
}