在TestNG中跳过测试的最佳实践是什么?

问题描述 投票:3回答:1

我几个月前才开始使用TestNG。我想跳过一个@Test,它有一个相关的错误等待修复,而无需忽略它(即,不将其标记为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处理此问题的常规做法?

(这是我的第一篇文章,如果我找不到已经存在的答案,请原谅我。

java automation testng
1个回答
0
投票

[在使用Listeners和其他方法尝试了不同的事情之后,我得出的结论是,据我所知,我能做的最好的事情就是添加一个将在超类或实用程序中定义的静态方法。包装:

@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)
© www.soinside.com 2019 - 2024. All rights reserved.