TestNG的用例除了直接运行之外,还可以使用代码来调用,这样做的好处在于我们可以将其嵌入其他代码中,来执行这些TestNG用例,方法如下:

1、直接调用用例类

范例:
定义了两个测试用例类为DependTest1.javaFactoryTest.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用例执行的更多相关文章

  1. Matlab与C/C++联合编程之Matlab以MEX方式调用C代码(五)完整过程加示

    如下为本人亲证代码: 一: 编译器的安装与配置(环境不同,显示结果不同) 要使用MATLAB编译器,用户计算机上应用事先安装与MATLAB适配的以下任何一种ANSI C/C++编译器: 5.0.6.0 ...

  2. 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代码方式来 ...

  3. 14、testng.xml 设置用例执行顺序

    目录如下: TestGroup.java 代码如下: package com.testng.cn; import org.testng.annotations.*; import static org ...

  4. YbSoftwareFactory 代码生成插件【二十五】:Razor视图中以全局方式调用后台方法输出页面代码的三种方法

    上一篇介绍了 MVC中实现动态自定义路由 的实现,本篇将介绍Razor视图中以全局方式调用后台方法输出页面代码的三种方法. 框架最新的升级实现了一个页面部件功能,其实就是通过后台方法查询数据库内容,把 ...

  5. AutoCAD.NET 不使用P/Invoke方式调用acad.exe或accore.dll中的接口(如acedCommand、acedPostCommand等)

    使用C#进行AutoCAD二次开发,有时候由于C#接口不够完善,或者低版本AutoCAD中的接口缺少,有些工作不能直接通过C#接口来实现,所以需要通过P/Invoke的方式调用AutoCAD的其他DL ...

  6. (转)将wcf 以webservice的方式调用

    将wcf 以webservice的方式调用 问题:a公司使用wcf 发布服务(.net Framework 3.0 or 3.5),b公司需要使用a公司发布的服务 ,但b公司目前阶段只使用.net F ...

  7. WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载]

    原文:WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载] 我们有两种典型的WCF调用方式:通过SvcUtil.exe(或者添加Web引用)导入发布的服务元数据生成服务代理相关的代码 ...

  8. SNF快速开发平台MVC-各种级联绑定方式,演示样例程序(包含表单和表格控件)

    做了这么多项目,经常会使用到级联.联动的情况. 如:省.市.县.区.一级分类.二级分类.三级分类.仓库.货位. 方式:有表单需要做级联的,还是表格行上需要做级联操作的. 实现:实现方法也有很多种方式. ...

  9. RTX——第19章 SVC 中断方式调用用户函数(后期补历程)

    以下内容转载自安富莱电子: http://forum.armfly.com/forum.php 本章节为大家讲解如何采用 SVC 中断方式调用用户函数. 当用户将 RTX 任务设置为工作在非特权级模式 ...

随机推荐

  1. python之参数解析模块argparse

    2.7之后python不再对optparse模块进行扩展,python标准库推荐使用argparse模块对命令行进行解析. 简单入门 先来看个例子: argparse_test.py: import ...

  2. LeetCode 783. 二叉搜索树结点最小距离(Minimum Distance Between BST Nodes)

    783. 二叉搜索树结点最小距离 LeetCode783. Minimum Distance Between BST Nodes 题目描述 给定一个二叉搜索树的根结点 root, 返回树中任意两节点的 ...

  3. Spring Boot制作启动图案

    SpringBoot在启动时会有一个默认图案的,如果不喜欢可以自己制作一个. 在resources的目录下新建banner.txt文件. 制作图案地址:springboot启动图案定制 通过输入字符串 ...

  4. [Asp.Net Core] - 设置 Syncfusion / RichTextEditor 移除超链接及高度自适应 的方法

    背景 使用 Syncfusion / RichTextEditor 对录入后的信息进行展示:1. 希望内容高度自适应.2. 希望禁用原文中的超链接.   实现 <div class=" ...

  5. CentOS 7 下安装 MySQL 5.7

    从 CentOS 7 系统开始,MariaDB 成为 yum 源中默认的数据库安装包.在 CentOS 7 及以上的系统中使用 yum 安装 MySQL 包将无法使用 MySQL.您可以选择使用完全兼 ...

  6. SpringBoot项目集成PageHelper使用

    SpringBoot项目集成PageHelper使用 一.开始 ​ 地址:https://github.com/pagehelper/Mybatis-PageHelper ​ 在spring boot ...

  7. Hadoop 完全分布式搭建

    搭建环境 https://www.cnblogs.com/YuanWeiBlogger/p/11456623.html 修改主机名------------------- 1./etc/hostname ...

  8. 基于openfire的IM即时通讯软件开发

    openfire:http://www.igniterealtime.org/ Xmpp:http://xmpp.org/ IOS(xmppframework):https://github.com/ ...

  9. vue 下拉刷新数据的插件的使用:

    1.安装: npm i vue-scroller -S npm install vue-scroller -D 2.在需要加载的页面中引入,或在公共js文件中引入: import VueScrolle ...

  10. yum更换源配置

    今天安装mysql5.7的时候出现了点问题,最后更换yum源解决了,把这个记录一下 yum源配置(阿里云源) 1) 安装wget yum install -y wget 2) 备份/etc/yum.r ...