testng 忽略测试_TestNG忽略或禁用测试

testng 忽略测试

Sometimes we want to ignore a TestNG test method, there are many ways to achieve this.

有时我们想忽略TestNG测试方法,有很多方法可以实现。

TestNG @Test启用参数 (TestNG @Test enable parameter)

We can disable or ignore a test method by adding enabled=false to @Test annotation. Let’s look at a simple example of ignoring a test method using this.

我们可以通过在@Test注释中添加enabled=false来禁用或忽略测试方法。 让我们看一个忽略此方法的简单示例。

package com.journaldev.utils;

import org.testng.annotations.Test;

public class TestNGExample {

	@Test
	public void foo() {
		System.out.println("Running foo test");
	}
	
	@Test(enabled=false)
	public void bar() {
		System.out.println("Running bar test");
	}
	
	@Test(groups="zoo")
	public void zoo() {
		System.out.println("Running zoo test");
	}
	
	@Test(groups="test")
	public void base() {
		System.out.println("Running base test");
	}
}

When above test class is executed, we get following output confirming that bar() method didn’t get executed.

当执行上述测试类时,我们得到以下输出,确认未执行bar()方法。

[RemoteTestNG] detected TestNG version 6.14.3
Running base test
Running foo test
Running zoo test
PASSED: base
PASSED: foo
PASSED: zoo

This method has a huge demerit, every time we want to disable a method we need to do code change and compile our classes again.

此方法有很大的缺点,每当我们要禁用一个方法时,都需要进行代码更改并再次编译我们的类。

XML套件中的TestNG禁用测试 (TestNG Disable Tests in XML Suite)

TestNG XML suite files provide a lot of options. We can exclude test methods as well as groups from the execution. Let’s create a simple TestNG XML suite file where we will exclude foo method. We will also exclude zoo from the test execution.

TestNG XML套件文件提供了很多选项。 我们可以从执行中排除测试方法以及组。 让我们创建一个简单的TestNG XML套件文件,其中将排除foo方法。 我们还将把zoo从测试执行中排除。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="TestNG Excludes Test Suite" guice-stage="DEVELOPMENT">
	<groups>
		<run>
			<exclude name="zoo"></exclude>
		</run>
	</groups>
	<test thread-count="5" name="TestNG Excludes Test" verbose="2">
		<groups>
			<run>
				<exclude name="zoo"></exclude>
			</run>
		</groups>
		<classes>
			<class name="com.journaldev.utils.TestNGExample">
				<methods>
					<exclude name="foo"></exclude>
				</methods>
			</class>
		</classes>
	</test>
</suite>

Run above XML file as TestNG Suite and it will produce following output.

在XML文件上方运行TestNG Suite,它将产生以下输出。

Notice that we can exclude groups from execution at suite level as well as test level. Above XML code is showing both ways, however, in this case, it’s redundant and we can remove any one of those configurations.

注意,我们可以在套件级别以及测试级别将组排除在执行之外。 上面的XML代码显示了两种方式,但是,在这种情况下,它是多余的,我们可以删除其中的任何一种配置。

GitHub Repository. GitHub Repository下载示例代码。

翻译自: https://www.journaldev.com/21337/testng-ignore-or-disable-tests

testng 忽略测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值