【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 任务设置为工作在非特权级模式 ...
随机推荐
- java 时间的原生操作和工具类操作
package com.xc.test.dateoperation; import org.apache.commons.lang3.time.DateFormatUtils; import org. ...
- 基于Java API for WebSocket (JSR-356)的标准websocket客户端
maven依赖 springboot <dependency> <groupId>org.springframework.boot</groupId> <ar ...
- jquery分页展示控件:kkpager
kkpager v1.2 js分页展示控件,传入简单参数就能使用的分页效果控件 准备工作,引入js.css <script type="text/javascript" sr ...
- 【VS开发】MFC修改Opencv namedWindow的风格
当在MFC中应用opencv的窗口来显示图片.希望namedWindow创建的窗口能根据需要改变风格: //by dongchunxiao cv::namedWindow("windows1 ...
- 基于python的App UI自动化环境搭建
Android端Ui 自动化环境搭建 一,安装JDK.SDK 二,添加环境变量 Widows:1.系统变量→新建 JAVA_HOME 变量E:\Java\jdk1.7.0 jdk安装目录 2.系统变量 ...
- Mowing the Lawn【线性dp + 单调队列优化】
题目链接:https://ac.nowcoder.com/acm/contest/2652/G 题目大意:与上一篇博客 烽火传递 差不多. 1.一共n头羊,若超过m头连续的羊在一起,就会集体罢工,每头 ...
- 使用speedtest-cli测量服务器带宽
使用speedtest-cli测量服务器带宽,这个是python写的工具,很方便,只需要在服务器端安装即可. 1.安装python-pip # yum install python-pip –y 2. ...
- Python 实现微信小程序的用户登录
小程序可以通过官方提供的登录能力来获取用户身份的标示,具体文档可以参考 这里,通过流程时序可以看到,对于需要和前端配合的服务端开发,主要实现的就是通过小程序提供的 code 换取用户的 openid ...
- SSM整合-配置文件
使用工具:maven.idea.jdk8.mysql.tomcat9.0 初学ssm框架,配置文件的配置目录: 其中genera ...
- java FastJson的使用总结
1.前言 1.1.FastJson的介绍: JSON(javaScript Object Notation)是一种轻量级的数据交换格式.主要采用键值对({"name": " ...