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. Sqoop2 将hdfs中的数据导出到MySQL

    1.进入sqoop2终端: [root@master /]# sqoop2 2.为客户端配置服务器: sqoop:000> set server --host master --port 120 ...

  2. python flask框架学习(二)——第一个flask程序

    第一个flask程序 学习自:知了课堂Python Flask框架——全栈开发 1.用pycharm新建一个flask项目 2.运行程序 from flask import Flask # 创建一个F ...

  3. 【相机篇】从到FlyCapture2到Spinnaker

    从FlyCapture2 到 Spinnaker SDK的变换,可参见FLIR公司机器视觉的相机产品:https://www.flir.com/iis/machine-vision/ Spinnake ...

  4. 利用ldirectord实现lvs后端realserver健康状态检查

    ldirectord用来实现LVS负载均衡资源在主.备节点间的故障转移.在首次启动时,ldirectord可以自动创建IPVS表.此外,它还可以监控各RealServer的运行状态,一旦发现某Real ...

  5. curl --resolve 查看证书情况

    通过curl  解析证书 [root@harbor ~]# curl --resolve 'www.abc.com:127.0.0.1' https://www.abc.com/ -vvv * Cou ...

  6. Django 之memcached的应用

    memcached介绍: memcached之前是danga的一个项目,最早是为LiveJournal服务的,当初设计师为了加速LiveJournal访问速度而开发的,后来被很多大型项目采用.官网是w ...

  7. C罗是你人生中最好的健身教练和精神导师

    C罗又进球了,两场小组赛包揽全队4粒进球,一己之力帮助葡萄牙取得1胜1平,掌握出线主动权.此前三届世界杯金靴分别只有6球.5球.5球进账,C罗如果能延续火爆状态,金靴唾手可得. 之前三届世界杯,C罗7 ...

  8. python 工具的URL

    Python取得大数据之后如何把数据图形化,之后让客户很清晰的看到你的结果 下面的图形化参照 matplotlib.3.0.2 https://matplotlib.org/gallery/index ...

  9. 在Jetty中部署Jenkins遇到的问题

    1. Jetty 9.0.3 启动时的错误: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 [root@kvm-guest jetty-9.0.3]# java -jar star ...

  10. mysql查看正在运行的语句

    mysql查看正在运行的语句 并且查看运行最多的mysql语句 MySQL 打开 general log 后,所有的查询语句都会记录在 general log 文件,文件为只读方式,但这样genera ...