【TestNG】使用代码方式调用TestNG用例执行
TestNG的用例除了直接运行之外,还可以使用代码来调用,这样做的好处在于我们可以将其嵌入其他代码中,来执行这些TestNG用例,方法如下:
1、直接调用用例类
范例:
定义了两个测试用例类为DependTest1.java和FactoryTest.java:
再做一个main函数来调用,代码如下:
package com.demo.test.testng;
import org.testng.TestNG;
public class Entry {
public static void main(String[] args) {
TestNG testNG = new TestNG();
Class[] classes = {DependTest1.class, FactoryTest.class};
testNG.setTestClasses(classes);
testNG.run();
}
}
执行结果如下:
[TestNG] Running:
Command line suite
dependTest1
dependTest2
dependTest4
login, host:10.10.10.1;port8080
login, host:10.10.10.2;port8080
logout, host:10.10.10.1;port8080
logout, host:10.10.10.2;port8080
===============================================
Command line suite
Total tests run: 7, Failures: 0, Skips: 0
===============================================
2、调用用例的xml文件
方法如下:
package com.demo.test.testng;
import java.util.ArrayList;
import java.util.List;
import org.testng.TestNG;
public class Entry {
public static void main(String[] args) {
TestNG testNG = new TestNG();
List<String> suites = new ArrayList<String>();
suites.add("D:\\software\\workspace\\testng\\src\\main\\java\\com\\demo\\test\\testCase\\depend1.xml");//此处为xml的绝对路径
testNG.setTestSuites(suites);
testNG.run();
}
运行结果如下:
...
... TestNG 6.10 by Cédric Beust (cedric@beust.com)
...
[TestRunner] Running the tests in 'Test' with parallel mode:none
[RunInfo] Adding method selector: org.testng.internal.XmlMethodSelector@17d10166 priority: 10
[TestClass] Creating TestClass for [ClassImpl class="com".demo.test.testng.FactoryTest]
[TestNG] Running:
D:\software\workspace\testng\src\main\java\com\demo\test\testCase\depend1.xml
[SuiteRunner] Created 1 TestRunners
[TestRunner] Running test Test on 1 classes, included groups:[] excluded groups:[]
===== Test class
com.demo.test.testng.FactoryTest
@Test FactoryTest.logout()[pri:0, instance:com.demo.test.testng.FactoryTest@6a6824be]
@Test FactoryTest.logout()[pri:0, instance:com.demo.test.testng.FactoryTest@5c8da962]
@Test FactoryTest.login()[pri:0, instance:com.demo.test.testng.FactoryTest@6a6824be]
@Test FactoryTest.login()[pri:0, instance:com.demo.test.testng.FactoryTest@5c8da962]
======
[Invoker 317983781] Invoking com.demo.test.testng.FactoryTest.login
login, host:10.10.10.2;port8080
[Invoker 317983781] Invoking com.demo.test.testng.FactoryTest.logout
logout, host:10.10.10.2;port8080
[Invoker 317983781] Invoking com.demo.test.testng.FactoryTest.login
login, host:10.10.10.1;port8080
[Invoker 317983781] Invoking com.demo.test.testng.FactoryTest.logout
logout, host:10.10.10.1;port8080
===== Invoked methods
FactoryTest.login()[pri:0, instance:com.demo.test.testng.FactoryTest@5c8da962] 1552787810
FactoryTest.logout()[pri:0, instance:com.demo.test.testng.FactoryTest@5c8da962] 1552787810
FactoryTest.login()[pri:0, instance:com.demo.test.testng.FactoryTest@6a6824be] 1785210046
FactoryTest.logout()[pri:0, instance:com.demo.test.testng.FactoryTest@6a6824be] 1785210046
=====
[[Utils]] Attempting to create D:\software\workspace\testng\test-output\SuiteDepend\Test.xml
[[Utils]] Directory D:\software\workspace\testng\test-output\SuiteDepend exists: true
Creating D:\software\workspace\testng\test-output\SuiteDepend\Test.xml
PASSED: login
PASSED: logout
PASSED: login
PASSED: logout
===============================================
Test
Tests run: 4, Failures: 0, Skips: 0
===============================================
这个例子的log要比上面那个详细很多,是因为我在xml中定义了日志详细等级;
3、自定义XmlSuite
除了直接书写xml外,还可以直接在代码中生成,方法如下:
譬如有个xml是这么写的:
<suite name="TmpSuite" >
<test name="TmpTest" >
<classes>
<class name="test.failures.Child" />
<classes>
</test>
</suite>
其对应的代码如下:
XmlSuite suite = new XmlSuite();
suite.setName("TmpSuite");
XmlTest test = new XmlTest(suite);
test.setName("TmpTest");
List<XmlClass> classes = new ArrayList<XmlClass>();
classes.add(new XmlClass("test.failures.Child"));
test.setXmlClasses(classes) ;
备注:其他譬如设置verbose等级等都有对应的方法可以使用
这个XmlSuite可以直接加入TestNG中运行,如下为使用该XmlSuite运行的代码:
List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.run();
4、设定group
那么如果想要只运行其中的几个group如何设置呢,如下:
package com.demo.test.testng;
import org.testng.TestNG;
public class Entry {
public static void main(String[] args) {
TestNG testNG = new TestNG();
testNG.setVerbose(3);
testNG.setTestClasses(new Class[]{DependTest1.class, FactoryTest.class});
testNG.setGroups("dependGroup1");//此处可以设置多个group名称,以逗号分隔
testNG.run();
}
如上面的代码,只设置group名称是无法运行的,必须先加载用例,可以是class也可以是xml,另外根据源码可知group名称是可以添加多个的,以,分隔即可;
如果只是某些group不想运行,则可以用方法testNG.setExcludedGroups(groups);同理这个groups同样是可以包含一个或者多个group名称,以,分隔;
5、设置输出目录
如果想要修改报告输出目录,则可以用如下代码来设置:
package com.demo.test.testng;
import org.testng.TestNG;
public class Entry {
public static void main(String[] args) {
TestNG testNG = new TestNG();
testNG.setVerbose(3);
testNG.setTestClasses(new Class[]{DependTest1.class, FactoryTest.class});
testNG.setGroups("dependGroup1");//此处可以设置多个group名称,以逗号分隔
testNG.setOutputDirectory("D:\\test");//设置输出目录
testNG.run();
}
这样TestNG就会把报告输出到此文件夹下;
6、调用jar包中的xml
如果我们把带有TestNG用例的工程打成了jar包,然后要在其他工程里调用这个jar包中的用例,可以使用如下方法:
TestNG testNG = new TestNG();
testNG.setTestJar(jarPath);//jar包的路径
testNG.setXmlPathInJar(xmlPathInJar);//jar包中xml文件的路径
7、其他方法
除了上面所说的那些之外,TestNG对象还有其他很多方法,譬如设置verbose,设置并发数等;
【TestNG】使用代码方式调用TestNG用例执行的更多相关文章
- Matlab与C/C++联合编程之Matlab以MEX方式调用C代码(五)完整过程加示
如下为本人亲证代码: 一: 编译器的安装与配置(环境不同,显示结果不同) 要使用MATLAB编译器,用户计算机上应用事先安装与MATLAB适配的以下任何一种ANSI C/C++编译器: 5.0.6.0 ...
- WCF入门教程(四)通过Host代码方式来承载服务 一个WCF使用TCP协议进行通协的例子 jquery ajax调用WCF,采用System.ServiceModel.WebHttpBinding System.ServiceModel.WSHttpBinding协议 学习WCF笔记之二 无废话WCF入门教程一[什么是WCF]
WCF入门教程(四)通过Host代码方式来承载服务 Posted on 2014-05-15 13:03 停留的风 阅读(7681) 评论(0) 编辑 收藏 WCF入门教程(四)通过Host代码方式来 ...
- 14、testng.xml 设置用例执行顺序
目录如下: TestGroup.java 代码如下: package com.testng.cn; import org.testng.annotations.*; import static org ...
- YbSoftwareFactory 代码生成插件【二十五】:Razor视图中以全局方式调用后台方法输出页面代码的三种方法
上一篇介绍了 MVC中实现动态自定义路由 的实现,本篇将介绍Razor视图中以全局方式调用后台方法输出页面代码的三种方法. 框架最新的升级实现了一个页面部件功能,其实就是通过后台方法查询数据库内容,把 ...
- AutoCAD.NET 不使用P/Invoke方式调用acad.exe或accore.dll中的接口(如acedCommand、acedPostCommand等)
使用C#进行AutoCAD二次开发,有时候由于C#接口不够完善,或者低版本AutoCAD中的接口缺少,有些工作不能直接通过C#接口来实现,所以需要通过P/Invoke的方式调用AutoCAD的其他DL ...
- (转)将wcf 以webservice的方式调用
将wcf 以webservice的方式调用 问题:a公司使用wcf 发布服务(.net Framework 3.0 or 3.5),b公司需要使用a公司发布的服务 ,但b公司目前阶段只使用.net F ...
- WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载]
原文:WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载] 我们有两种典型的WCF调用方式:通过SvcUtil.exe(或者添加Web引用)导入发布的服务元数据生成服务代理相关的代码 ...
- SNF快速开发平台MVC-各种级联绑定方式,演示样例程序(包含表单和表格控件)
做了这么多项目,经常会使用到级联.联动的情况. 如:省.市.县.区.一级分类.二级分类.三级分类.仓库.货位. 方式:有表单需要做级联的,还是表格行上需要做级联操作的. 实现:实现方法也有很多种方式. ...
- RTX——第19章 SVC 中断方式调用用户函数(后期补历程)
以下内容转载自安富莱电子: http://forum.armfly.com/forum.php 本章节为大家讲解如何采用 SVC 中断方式调用用户函数. 当用户将 RTX 任务设置为工作在非特权级模式 ...
随机推荐
- 123457123456#0#-----com.ppGame.huaHua65--前拼后广--儿童填色-pp
com.ppGame.huaHua65--前拼后广--儿童填色-pp
- Python web-Http
web应用 Web应用程序一般指浏览器端/服务器端应用程序,这类应用程序一般借助谷歌,火狐等浏览器来运行.在网络编程的意义下,浏览器是一个socket客户端,服务器是一个socket服务端 impor ...
- 改进初学者的PID-介绍
最近看到了Brett Beauregard发表的有关PID的系列文章,感觉对于理解PID算法很有帮助,于是将系列文章翻译过来!在自我提高的过程中,也希望对同道中人有所帮助.作者Brett Beaure ...
- 【 Linux 】Systemd 配置文件说明及编写(2)
1. 开机启动 对于支持 systemd 的软件,如果想要设置开机启动,就执行如下命令(以 http 为例): systemctl enable httpd 上面的命令相当于在 /etc/system ...
- 【JS新手教程】JS修改css样式的几种方法
本文试验了几种JS修改css样式的方法,方法1:元素.style.样式=样式值.方法2:元素.style.cssText=样式:样式值方法3:元素.style[样式]=样式值 .cssText这种,可 ...
- 【JQuery插件】团购倒计时
案例截图 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3. ...
- Excel对某一列的数据插入处理,域名得出IP
早期都是通过Excel做数据统计,对某一列的数据插入处理. 代码功能是从A列纯域名,将域名转换为IP,从域名A列得到IP写到B列. 代码 #!/usr/bin/python #coding:utf-8 ...
- RabbitMQ官方教程二 Work Queues(GOLANG语言实现)
RabbitMQ官方教程二 Work Queues(GOLANG语言实现) 在第一个教程中,我们编写了程序来发送和接收来自命名队列的消息. 在这一部分中,我们将创建一个工作队列,该队列将用于在多个wo ...
- SSM整合-配置文件
使用工具:maven.idea.jdk8.mysql.tomcat9.0 初学ssm框架,配置文件的配置目录: 其中genera ...
- Ubuntu 更换下载源
Ubuntu将下载官方源更换为国内源 由于某些原因,在国内更新软件都很慢,可以改源为国内源 1.备份原始文件 sudo cp /etc/apt/sources.list /etc/apt/source ...