我几个月前才开始使用 TestNG。我想跳过一个 @Test,它有一个等待修复的 bug,而不忽略它(也就是说,不将其标记为 enabled=false),因为我仍然希望它在报告中显示为已跳过。
我也有那个测试的依赖项,所以如果我只是忽略@Test,那么我将不得不更改所有依赖项。我更喜欢跳过测试,因此也将跳过依赖测试。
我目前正在尝试做:
@Test(enabled = true, groups = { "GroupA" }, dependsOnMethods = { "MethodB" } )
public void testA() {
throw new SkipException("Test blocked due to Bug-1234");
doTest(); // Compile error: Unreachable code.
}
一个快速的解决方案是注释该代码,但这会产生很多警告,而且看起来不太好。
我也可以在 Skip 之前添加一个 if(true),就像我在其他解决方案中看到的那样,但这会生成一个我希望避免的无效代码警告。
我的问题不是“我能做什么?”,而是“你会做什么?”。是否有使用 TestNG 处理此问题的通用做法?
(这是我的第一篇文章,如果我没有找到已经存在的答案,请原谅我)。
最佳答案
在用 Listener 和其他一些方法尝试了不同的事情之后,我得出的结论是,据我所知,我能做的最好的事情就是添加一个将在父类(super class)或类中定义的静态方法实用程序包:
@Test( enabled = true,
description = "Validates that it is possible to create a new booking.")
public void validateCreateNewBooking() {
skipTest("BUG-1234");
doTest();
}
其中skipTest:
public static void skipTest(String reason) {
throw new SkipException("Test voluntarily skipped. Reason: " + reason);
}
所以结果:
SKIPPED: validateCreateNewBooking
Validates that it is possible to create a new booking.
org.testng.SkipException: Test voluntarily skipped. Reason: BUG-1234
at com.openjaw.testregression.tretailapi.test.TestConfig.skipTest(TestConfig.java:28)
at com.openjaw.testregression.tretailapi.test.booking.CreateBookingTest.validateCreateNewBooking(CreateBookingTest.java:16)
关于java - 在 TestNG 中跳过测试的最佳做法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58713723/