testng 组_TestNG组

testng 组

TestNG Groups is one of its very important and useful features. We can specify groups for TestNG methods, it can be used with @Before, @After and @Test methods.

TestNG Groups是其非常重要和有用的功能之一。 我们可以为TestNG方法指定组,它可以与@ Before,@ After和@Test方法一起使用。

TestNG组 (TestNG Groups)

Once we have defined the groups for TestNG methods, we can run specific groups. We can include groups to be executed, exclude groups. We can also create a group of groups. Let’s create a TestNG test class with few methods and assign them to different groups.

一旦为TestNG方法定义了组,就可以运行特定的组。 我们可以包括要执行的组,不包括组。 我们还可以创建一组组。 让我们用很少的方法创建一个TestNG测试类,并将它们分配给不同的组。

package com.journaldev.groups;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TestNGGroupsExample {

	@Test(groups = "foo")
	public void foo() {
		System.out.println("foo");
	}

	@Test(groups = "bar")
	public void bar() {
		System.out.println("bar");
	}

	@Test(groups = { "alpha", "sanity" })
	public void alpha() {
		System.out.println("alpha");
	}

	@Test(groups = { "beta", "integration" })
	public void beta() {
		System.out.println("beta");
	}

	@BeforeClass(groups = "integration")
	public void beforeIntegrationTests() {
		System.out.println("Before Running integration test methods");
	}

	@AfterClass(groups = "integration")
	public void afterIntegrationTests() {
		System.out.println("Before Running integration test methods");
	}

}

使用Eclipse TestNG运行配置运行TestNG组 (Running TestNG Groups using Eclipse TestNG Run Configuration)

When we execute a TestNG class in Eclipse, it executes all the groups. We can create a Run Configuration for specific groups to execute. Go to Run | Run Configurations.

当我们在Eclipse中执行TestNG类时,它将执行所有组。 我们可以为特定的组创建一个运行配置以执行。 去运行| 运行配置

Browse for your project, then select the Groups checkbox and click on Browse button. It will show you all the groups defined in the project.

浏览您的项目,然后选择“组”复选框,然后单击“浏览”按钮。 它将向您显示项目中定义的所有组。

Select the groups to execute and you should see configuration like below image.

选择要执行的组,您将看到下图所示的配置。

Click on “Apply” button to save the configuration. Click on “Run” button to run the configuration, which will execute only the specified groups.

单击“应用”按钮以保存配置。 单击“运行”按钮运行配置,它将仅执行指定的组。

You should get following output in the Eclipse console.

您应该在Eclipse控制台中获得以下输出。

[RemoteTestNG] detected TestNG version 6.14.3
bar
foo
PASSED: bar
PASSED: foo

===============================================
    GRP-bar,foo
    Tests run: 2, Failures: 0, Skips: 0
===============================================


===============================================
TestNG-Examples by groups
Total tests run: 2, Failures: 0, Skips: 0
===============================================

TestNG XML Suite组示例 (TestNG XML Suite Groups Example)

We can configure our test suite to run specific groups using groups element. Below TestNG XML suite will run only foo and bar group methods.

我们可以配置测试套件以使用groups元素运行特定的组。 在TestNG XML套件下面,将仅运行foobar组方法。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="TestNGXMLTest Test Suite">
	<groups>
		<run>
			<include name="foo"></include>
			<include name="bar"></include>
		</run>
	</groups>
	<test name="TestNGXMLTest Test">
		<classes>
			<class name="com.journaldev.groups.TestNGGroupsExample" />
		</classes>
	</test>
</suite>

TestNG XML包含排除组示例 (TestNG XML Include Exclude Groups Example)

Sometimes our methods will be part of multiple groups. If the method is part of both included and excluded groups, then it will be excluded from execution.

有时,我们的方法将成为多个组的一部分。 如果该方法是包含组和排除组的一部分,则将其从执行中排除。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="TestNGXMLTest Test Suite">
	<groups>
		<run>
			<include name="alpha"></include>
			<include name="beta"></include>
			<exclude name="integration"></exclude>
		</run>
	</groups>
	<test name="TestNGXMLTest Test">
		<classes>
			<class name="com.journaldev.groups.TestNGGroupsExample" />
		</classes>
	</test>
</suite>

Above test suite will not execute beta() method because one of its groups is in excluded groups list.

上面的测试套件将不会执行beta()方法,因为其组之一在排除组列表中。

TestNG组正则表达式 (TestNG Groups Regular Expression)

TestNG groups configuration supports regular expression. If we change groups include to include name=".*o.*", then it will only execute foo() and beta() methods. It’s because they belong to the groups whose name conatins letter “o”.

TestNG组配置支持正则表达式。 如果我们将包括的组更改为include name=".*o.*" ,则它将仅执行foo()和beta()方法。 这是因为它们属于名称包含字母“ o”的组。

TestNG默认组 (TestNG Default Group)

If we want all our Test class methods to be part of a specific group, we can configure it at the class level.

如果我们希望所有Test类方法都属于特定组,则可以在类级别对其进行配置。

@Test(groups="default")
public class TestNGGroupsExample {
// test methods
}

TestNG组组 (TestNG Group of Groups)

We can define group of groups in the TestNG XML file. Then we can use it in the test suite. Note that this can be done inside only at test level, not at suite level.

我们可以在TestNG XML文件中定义组组。 然后,我们可以在测试套件中使用它。 请注意,只能在测试级别而不是套件级别内部进行此操作。

Let’s look at a quick example of defining groups in the TestNG XML file and then using them.

让我们看一个在TestNG XML文件中定义组然后使用它们的快速示例。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="TestNGXMLTest Test Suite">

	<test name="TestNGXMLTest Test">
		<groups>
			<define name="include-groups">
				<include name="sanity" />
				<include name="integration" />
			</define>
			<define name="exclude-groups">
				<include name="foo" />
				<include name="bar" />
			</define>
			<run>
				<include name="include-groups" />
				<exclude name="exclude-groups" />
			</run>
		</groups>
		<classes>
			<class name="com.journaldev.groups.TestNGGroupsExample" />
		</classes>
	</test>
</suite>

That’s all for TestNG groups example. They are very helpful in categorizing our tests and run them for different levels of verification.

这就是TestNG组示例的全部内容。 它们对我们的测试进行分类并为不同级别的验证运行它们非常有帮助。

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

翻译自: https://www.journaldev.com/21349/testng-groups

testng 组

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值