码迷,mamicode.com
首页 > 其他好文 > 详细

TestNG的组测试和组中组测试

时间:2014-10-21 17:41:41      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:testng分组测试   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的组测试和组中组测试

标签:testng分组测试   testng组中组测试   

原文地址:http://blog.csdn.net/wanghantong/article/details/40347945

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!