TestNG – 如何禁用/忽略测试方法

TestNG – 如何禁用/忽略测试方法

原文: https://howtodoinjava.com/testng/testng-how-to-disableignore-test-method/

在执行 TestNG 测试时,可能在某些情况下可能需要禁用特定测试或一组测试才能执行。 例如,考虑由于某些测试属于某些无法执行的场景而导致功能中存在严重错误的场景。 由于问题已经被发现,我们可能需要禁止执行上述测试方案

禁用 TestNG 中的测试可以通过将@Test注解的enable属性设置为false来实现。 这将使所述测试方法无法作为测试套件的一部分执行。 如果在类级别为Test注解设置了此属性,则将禁用该类内的所有公共方法。

@Test( enabled=false )

让我们看一个例子,以更好地理解它。

禁用测试方法的示例

在下面的测试中,我们有三种测试方法,即testMethodOne()testMethodTwo()testMethodThree()。 其中testMethodTwo()需要禁用。

public class DisableTestDemo 
{
	@Test(enabled = true)
	public void testMethodOne() {
		System.out.println("Test method one.");
	}

	@Test(enabled = false)
	public void testMethodTwo() {
		System.out.println("Test method two.");
	}

	@Test
	public void testMethodThree() {
		System.out.println("Test method three.");
	}
}

以上测试运行的输出如下:

[TestNG] Running:  C:\Users\somepath\testng-customsuite.xml

Test method one.
Test method three.

PASSED: testMethodOne
PASSED: testMethodThree

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================

如您在前面的结果中所看到的,TestNG 仅执行了两种方法。 测试执行将忽略属性启用值为false的方法。 默认情况下,属性启用值为true,因此即使未指定属性值,您也可以看到名称为testMethodThree()的测试方法已由 TestNG 执行。

学习愉快!