TestNG的组測试和组中组測试

在编写測试的过程中,我们常常遇到仅仅想运行个别或者某一部分/某一类型的測试用例,这时我们能够使用TestNG的分组測试方法

分组測试在配置时。TestNG运行的原则是:仅仅保留最小集合进行运行

看代码:

/**
 * 
 * <p>
 * Title: TestngGroups
 * </p>
 * 
 * <p>
 * 相应配置文件testng-groups.xml 
 * Description:使用groups进行分组測试,include和exclude的原则是保留最小集合,
 * </p>
 * 
 * <p>
 * Company:
 * </p>
 * 
 * @author : Dragon
 * 
 * @date : 2014年10月13日
 */
public class TestngGroups {
	@Test(groups = { "functest", "checkintest" })
	public void testMethod1() {
		System.err.println("groups = { functest, checkintest }");
	}

	@Test(groups = { "functest", "checkintest" })
	public void testMethod2() {
		System.err.println("groups = { functest, checkintest }");
	}

	@Test(groups = { "functest" })
	public void testMethod3() {
		System.err.println("groups = { functest }");
	}

	@Test(groups = { "checkintest" })
	public void testMethod4() {
		System.err.println("groups = { checkintest }");
	}

}

配置文件:testng-groups.xml

<?

xml version="1.0" encoding="UTF-8"?

> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="framework_testng"> <test verbose="2" name="TestGroups"> <groups> <run> <include name="functest" /> <exclude name="checkintest" /> </run> </groups> <classes> <class name="com.dragon.testng.annotation.TestngGroups" /> </classes> </test> </suite>


运行结果:

groups = { functest }
PASSED: testMethod3

===============================================
    TestGroups
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
framework_testng
Total tests run: 1, Failures: 0, Skips: 0
===============================================

当我们的測试用例累积了非常多以后,我们可能不须要測试之前的分组,仅仅要測试刚刚写好的分组,这时候testng提供了一种新的配置方式。来实现这一功能,让測试人员仅仅改动配置文件就完毕測试

注意:多个group測试时,xml文件dom顺序必须是'<groups>'标签必须在'<test>'标签内, 否则会 有空指针异常

/**
 * 
 * <p>
 * Title: TestngGroupsOfGroups
 * </p>
 * 
 * <p>
 * 參考配置文件:testng-groupsOfGroups.xml
 * Description:使用<define>标签将測试方法在组内再次进行分组并以name属性进行区分,
 * <run>通过define标签的name进行调用,以后改动測试直接改动run调用的名称就可以
 * 
 * 注:<b>多个group測试时,xml文件dom顺序必须是'<groups>'标签必须在'<test>'标签内, 否则会 有空指针异常
 * </p>
 * 
 * <p>
 * Company:
 * </p>
 * 
 * @author : Dragon
 * 
 * @date : 2014年10月13日
 */
public class TestngGroupsOfGroups {

	@Test(groups = { "windows.xp" })
	public void testMethod5() {
		System.err.println("(groups = { windows.xp })");
	}

	@Test(groups = { "windows.7" })
	public void testMethod6() {
		System.err.println("(groups = { windows.7 })");
	}

	@Test(groups = { "windows.8" })
	public void testMethod7() {
		System.err.println("(groups = { windows.8 })");
	}
}
配置文件:testng-groupOfGroups.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="framework_testng">
	<test verbose="2" name="TestGroupsOfGroups">
		<groups>
			<define name="windows.xp">
				<include name="windows.xp" />
			</define>
			<define name="windows.7">
				<include name="windows.7" />
			</define>
			<define name="all">
				<include name="windows.*" />
			</define>
			<run>
				<include name="all" />
				<exclude name="windows.7" />
			</run>
		</groups>
		<classes>
			<class name="com.dragon.testng.annotation.TestngGroupsOfGroups" />
		</classes>
	</test>
</suite>

測试结果:(注意:此时 被执行的測试分组将在run标签内进行配置,include和exclude时,是依据Define标签的name来决定)

(groups = { windows.xp })
(groups = { windows.8 })
PASSED: testMethod5
PASSED: testMethod7

===============================================
    TestGroupsOfGroups
    Tests run: 2, Failures: 0, Skips: 0
===============================================


TestNG的參数化測试、共享线程池配置、參数默认值配置



假设我宽容,

别觉得我怯懦。由于我明确,宽容是美德,美德没有错
原文地址:https://www.cnblogs.com/zhchoutai/p/6734712.html