- 浏览: 75162 次
文章分类
tesng教程:https://www.yiibai.com/testng
1、依靠group分组忽略某些方法执行
testNG通过group属性实现分组,然后在testng.xml中通过<groups>标签指定哪个组的方法运行,哪个不运行。分为方法分组和类分组,下面用代码示例说明。
方法分组:
<code="java">
package com.test.groups;
import org.testng.annotations.Test;
public class GroupsOnMethod {
@Test(groups = "server")
public void serverTest1() {
System.out.println("这是服务端组测试方法1");
}
@Test(groups = "server")
public void serverTest2() {
System.out.println("这是服务端组测试方法2");
}
@Test(groups = "client")
public void clientTest1() {
System.out.println("这是客户端组测试方法1111");
}
@Test(groups = "client")
public void clientTest2() {
System.out.println("这是客户端组测试方法2222");
}
@Test(groups = "thirdParty")
public void thirdPartyTest1() {
System.out.println("这是第三方组测试方法1111");
}
@Test(groups = "thirdParty")
public void thirdPartyTest2() {
System.out.println("这是第三方组测试方法2222");
}
}
</code>
类分组:
<code="java">
package com.test.groups;
import org.testng.annotations.Test;
@Test(groups = "stu")
public class GroupsOnClass1 {
public void stu1() {
System.out.println("GroupsOnClass1中的stu1运行!");
}
public void stu2() {
System.out.println("GroupsOnClass1中的stu2运行!");
}
}
</code>
<code="java">
package com.test.groups;
import org.testng.annotations.Test;
@Test(groups = "stu")
public class GroupsOnClass2 {
public void stu1() {
System.out.println("GroupsOnClass2中的stu1运行!");
}
public void stu2() {
System.out.println("GroupsOnClass2中的stu2运行!");
}
}
</code>
<code="java">
package com.test.groups;
import org.testng.annotations.Test;
@Test(groups = "teacher")
public class GroupsOnClass3 {
public void teacher1() {
System.out.println("GroupsOnClass3中的teacher1运行!");
}
public void teacher2() {
System.out.println("GroupsOnClass3中的teacher2运行!");
}
}
</code>
在testng.xml中如下配置:
<code="java">
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="groupSuite" preserve-order="true">
<test name="groupOnMethodTest">
<groups>
<run>
<!-- 不包含某个分组 -->
<exclude name="client"/>
</run>
</groups>
<classes>
<class name="com.janson.groups.GroupsOnMethod"/>
</classes>
</test>
<test name="groupOnClassTest">
<groups>
<run>
<!-- 包含某个分组 -->
<include name="teacher"/>
</run>
</groups>
<classes>
<class name="com.janson.groups.GroupsOnClass1"/>
<class name="com.janson.groups.GroupsOnClass2"/>
<class name="com.janson.groups.GroupsOnClass3"/>
</classes>
</test>
</suite>
</code>
执行结果如下:
这是服务端组测试方法1
这是服务端组测试方法2
这是第三方组测试方法1111
这是第三方组测试方法2222
GroupsOnClass3中的teacher1运行!
GroupsOnClass3中的teacher2运行!
2、通过enabled参数忽略某些方法
有时候某些测试方法暂时不需要执行,但是又不想删除代码,可能以后又需要执行,这时怎么办呢,我们可以使用testNG的忽略测试,就是在@Test中设置enabled属性为false,示例代码如下:
<code="java">
public class IgnoreTest {
@Test(enabled = false)
public void ignore1() {
System.out.println("ignore1 运行!");
}
@Test //默认是不忽略的
public void ignore2() {
System.out.println("ignore2 运行!");
}
@Test(enabled = true)
public void ignore3() {
System.out.println("ignore3 运行!");
}
}
</code>
执行结果如下:
ignore2 运行!
ignore3 运行!
3、通过groups设置依赖关系:
有时候某个测试方法要等其他某个测试方法执行完才能执行,可以使用dependsOnMethods指定要依赖的测试方法,这个只能用在某个类里面,指定依赖本类中的方法,而当需要依赖其他类的方法时,可以通过dependsOnGroups,代码如下:
<code="java">
package com.propertiesfile;
import org.testng.annotations.Test;
public class WaybillSelect {
@Test(groups= "one")
public void test1() {
System.out.println("this is test1");
}
@Test(groups= "one")
public void tester1() {
System.out.println("this is tester1");
}
@Test
public void test2() {
System.out.println("this is test2");
}
}
package com.moneyselect;
import org.testng.annotations.*;
public class MoneySelectTest {
@Test
public void test1() {
System.out.println("this is moneyselect.test1 method");
}
@Test//(dependsOnGroups= {"one"})
public void beforesuitetest() {
System.out.println("this is beforesuite test");
}
@Test
public void beforetest() {
System.out.println("this is beforetest test");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="zhanglutest" verbose="2" parallel="classes" >
<test name="testcase1">
<classes>
<class name="com.propertiesfile.WaybillSelect"></class>
<class name="com.moneyselect.MoneySelectTest"></class>
</classes>
</test>
</suite>
</code>
运行结果如下,如果不加依赖,因为设置了parallel="classes",所以两个类会在两个线程中按照各自的方法名字典升序排序运行,MoneySelectTest类中的方法顺序应该是beforesuitetest()、beforetest()、test1(),因为现在制定了beforesuitetest()要等待组名为one的组中的方法执行完后再执行,所以执行结果如下。
this is beforetest test
this is test1
this is tester1
this is moneyselect.test1 method
this is beforesuite test
this is test2
从上面的结果可以看出,添加了groups依赖后,WaybillSelect中的几个方法不是按照方法名字典顺序执行,而是先执行了设置了groups参数的方法,再执行没有该参数的方法,这个是为什么没有找到原因,难道是和依赖该group的方法有关?另外如果设置了多个groups,各个groups方法的执行顺序是怎样的也有待研究。
1、依靠group分组忽略某些方法执行
testNG通过group属性实现分组,然后在testng.xml中通过<groups>标签指定哪个组的方法运行,哪个不运行。分为方法分组和类分组,下面用代码示例说明。
方法分组:
<code="java">
package com.test.groups;
import org.testng.annotations.Test;
public class GroupsOnMethod {
@Test(groups = "server")
public void serverTest1() {
System.out.println("这是服务端组测试方法1");
}
@Test(groups = "server")
public void serverTest2() {
System.out.println("这是服务端组测试方法2");
}
@Test(groups = "client")
public void clientTest1() {
System.out.println("这是客户端组测试方法1111");
}
@Test(groups = "client")
public void clientTest2() {
System.out.println("这是客户端组测试方法2222");
}
@Test(groups = "thirdParty")
public void thirdPartyTest1() {
System.out.println("这是第三方组测试方法1111");
}
@Test(groups = "thirdParty")
public void thirdPartyTest2() {
System.out.println("这是第三方组测试方法2222");
}
}
</code>
类分组:
<code="java">
package com.test.groups;
import org.testng.annotations.Test;
@Test(groups = "stu")
public class GroupsOnClass1 {
public void stu1() {
System.out.println("GroupsOnClass1中的stu1运行!");
}
public void stu2() {
System.out.println("GroupsOnClass1中的stu2运行!");
}
}
</code>
<code="java">
package com.test.groups;
import org.testng.annotations.Test;
@Test(groups = "stu")
public class GroupsOnClass2 {
public void stu1() {
System.out.println("GroupsOnClass2中的stu1运行!");
}
public void stu2() {
System.out.println("GroupsOnClass2中的stu2运行!");
}
}
</code>
<code="java">
package com.test.groups;
import org.testng.annotations.Test;
@Test(groups = "teacher")
public class GroupsOnClass3 {
public void teacher1() {
System.out.println("GroupsOnClass3中的teacher1运行!");
}
public void teacher2() {
System.out.println("GroupsOnClass3中的teacher2运行!");
}
}
</code>
在testng.xml中如下配置:
<code="java">
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="groupSuite" preserve-order="true">
<test name="groupOnMethodTest">
<groups>
<run>
<!-- 不包含某个分组 -->
<exclude name="client"/>
</run>
</groups>
<classes>
<class name="com.janson.groups.GroupsOnMethod"/>
</classes>
</test>
<test name="groupOnClassTest">
<groups>
<run>
<!-- 包含某个分组 -->
<include name="teacher"/>
</run>
</groups>
<classes>
<class name="com.janson.groups.GroupsOnClass1"/>
<class name="com.janson.groups.GroupsOnClass2"/>
<class name="com.janson.groups.GroupsOnClass3"/>
</classes>
</test>
</suite>
</code>
执行结果如下:
这是服务端组测试方法1
这是服务端组测试方法2
这是第三方组测试方法1111
这是第三方组测试方法2222
GroupsOnClass3中的teacher1运行!
GroupsOnClass3中的teacher2运行!
2、通过enabled参数忽略某些方法
有时候某些测试方法暂时不需要执行,但是又不想删除代码,可能以后又需要执行,这时怎么办呢,我们可以使用testNG的忽略测试,就是在@Test中设置enabled属性为false,示例代码如下:
<code="java">
public class IgnoreTest {
@Test(enabled = false)
public void ignore1() {
System.out.println("ignore1 运行!");
}
@Test //默认是不忽略的
public void ignore2() {
System.out.println("ignore2 运行!");
}
@Test(enabled = true)
public void ignore3() {
System.out.println("ignore3 运行!");
}
}
</code>
执行结果如下:
ignore2 运行!
ignore3 运行!
3、通过groups设置依赖关系:
有时候某个测试方法要等其他某个测试方法执行完才能执行,可以使用dependsOnMethods指定要依赖的测试方法,这个只能用在某个类里面,指定依赖本类中的方法,而当需要依赖其他类的方法时,可以通过dependsOnGroups,代码如下:
<code="java">
package com.propertiesfile;
import org.testng.annotations.Test;
public class WaybillSelect {
@Test(groups= "one")
public void test1() {
System.out.println("this is test1");
}
@Test(groups= "one")
public void tester1() {
System.out.println("this is tester1");
}
@Test
public void test2() {
System.out.println("this is test2");
}
}
package com.moneyselect;
import org.testng.annotations.*;
public class MoneySelectTest {
@Test
public void test1() {
System.out.println("this is moneyselect.test1 method");
}
@Test//(dependsOnGroups= {"one"})
public void beforesuitetest() {
System.out.println("this is beforesuite test");
}
@Test
public void beforetest() {
System.out.println("this is beforetest test");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="zhanglutest" verbose="2" parallel="classes" >
<test name="testcase1">
<classes>
<class name="com.propertiesfile.WaybillSelect"></class>
<class name="com.moneyselect.MoneySelectTest"></class>
</classes>
</test>
</suite>
</code>
运行结果如下,如果不加依赖,因为设置了parallel="classes",所以两个类会在两个线程中按照各自的方法名字典升序排序运行,MoneySelectTest类中的方法顺序应该是beforesuitetest()、beforetest()、test1(),因为现在制定了beforesuitetest()要等待组名为one的组中的方法执行完后再执行,所以执行结果如下。
this is beforetest test
this is test1
this is tester1
this is moneyselect.test1 method
this is beforesuite test
this is test2
从上面的结果可以看出,添加了groups依赖后,WaybillSelect中的几个方法不是按照方法名字典顺序执行,而是先执行了设置了groups参数的方法,再执行没有该参数的方法,这个是为什么没有找到原因,难道是和依赖该group的方法有关?另外如果设置了多个groups,各个groups方法的执行顺序是怎样的也有待研究。
发表评论
-
testNG学习之超时设置和异常测试
2019-12-17 17:29 7681、设置测试方法的超时 ... -
testNG学习之参数化
2019-12-17 16:13 324TestNG的测试方法中可以传入参数,而参数的 ... -
testng控制测试用例执行顺序及并发测试
2019-12-14 19:09 10111、class的执行顺序 通过preserve- ... -
testNG依赖测试及设置多线程
2019-11-24 18:43 0https://www.cnblogs.com/janson0 ... -
testNG标签范围及多suite配置
2019-11-24 17:44 738一、基本注解及其作用范围 1、testNG的基本注解标签: @ ... -
sf-test
2015-10-10 15:57 0this is used for download somet ... -
junit一个测试方法测试多种数据
2014-08-17 17:50 2376有些方法参数有许多特殊值,或者说他的参数分为很多歌区域,为了简 ... -
JUnit4与TestNG对比
2013-12-29 13:16 1492最近构建自动化测试, ...
相关推荐
1、testng测试报告模板BeautifulReport 2、testng.xml文件
《Java测试新技术:TestNG和高级概念》介绍了Java测试的新技术,主要内容包括:基本概念、测试设计模式、企业级测试、Java EE测试、集成和扩展TestNG等。《Java测试新技术:TestNG和高级概念》通过针对有效测试Java...
《Java测试新技术TestNG和高级概念》介绍了Java测试的新技术,主要内容包括:基本概念、测试设计模式、企业级测试、Java EE测试、集成和扩展TestNG等。《Java测试新技术TestNG和高级概念》通过针对有效测试Java应用...
无论您使用TestNG,JUnit或其他测试框架,本书提供的测试设计模式都会告诉您如何改进您的测试,对如何让代码和设计变得更可测试提供具体的建议。 目录: 第1章 起步 1.1 超越JUnit 3 1.2 JUnit 4 1.3 针对可测试性而...
java+selenium+maven+testng自动化测试框架实例(实际项目)
适用于想了解TestNG测试的同学
本书主要内容包括: 展示了与测试有关的折衷考虑,这样您就能在测试...无论您使用TestNG,JUnit或其他测试框架,本书提供的测试设计模式都会告诉您如何改进您的测试,对如何让代码和设计变得更可测试提供具体的建议。
讲解如何使用Selenium 和 TestNG 进行编程式测试
开源自动化测试框架testng包,用于appium+java+android测试
java测试新技术TestNG和高级概念 TestNG是一个测试框架,其灵感来自JUnit和NUnit,但引入了一些新的功能,使其功能更强大,使用更方便。TestNG是一个开源自动化测试框架;TestNG表示下一代(Next Generation的首字母)...
本书主要内容包括: 展示了与测试有关的折衷考虑,这样您就能在测试...无论您使用TestNG,JUnit或其他测试框架,本书提供的测试设计模式都会告诉您如何改进您的测试,对如何让代码和设计变得更可测试提供具体的建议。
集成 Jenkins 和 TestNG 实现自助式自动化测试平台,测试可行,。
testng下一代测试框架
ant+selenium+testng自动化测试方案
1. 支持多语言多框架集成:包括java的testng、junit单元测试框架,和python的pytest等框架集成,满足当前行业里主流测试开发工程师手撕的自动化测试框架集成测试报告需求。 2. Allure测试报告支持中文、英文切换,且...
httpclient testgn 接口测试
TestNG-Abbot源自于两个成功的开发人员测试框架的结合:Abbot和TestNG。Abbot是一种JUnit扩展框架,主要目的是使GUI组件实现编程隔离,它还提供了一种验证GUI行为的简易方法。TestNG-Abbot是一种测试框架,它为GUI...
无论您使用TestNG,JUnit或其他测试框架,本书提供的测试设计模式都会告诉您如何改进您的测试,对如何让代码和设计变得更可测试提供具体的建议。 目录: 第1章 起步 1.1 超越JUnit 3 1.2 JUnit 4 1.3 针对可测试性而...
java测试新技术-TestNG和高级概念,详细阐述了TestNG的概念
全栈自动化测试实战 基于testng,httpclient,selenium.appium 书本上的东西