In this tutorial, we will show you how to run multiple TestNG test cases (classes) together, aka suite test.

1. Test Classes

Review following three test classes.

TestConfig.java
package com.mkyong.testng.examples.suite;

import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest; //show the use of @BeforeSuite and @BeforeTest
public class TestConfig { @BeforeSuite
public void testBeforeSuite() {
System.out.println("testBeforeSuite()");
} @AfterSuite
public void testAfterSuite() {
System.out.println("testAfterSuite()");
} @BeforeTest
public void testBeforeTest() {
System.out.println("testBeforeTest()");
} @AfterTest
public void testAfterTest() {
System.out.println("testAfterTest()");
} }
TestDatabase.java
package com.mkyong.testng.examples.suite;

import org.testng.annotations.Test;

public class TestDatabase {

	@Test(groups = "db")
public void testConnectOracle() {
System.out.println("testConnectOracle()");
} @Test(groups = "db")
public void testConnectMsSQL() {
System.out.println("testConnectMsSQL");
} @Test(groups = "db-nosql")
public void testConnectMongoDB() {
System.out.println("testConnectMongoDB");
} @Test(groups = { "db", "brokenTests" })
public void testConnectMySQL() {
System.out.println("testConnectMySQL");
} }
TestOrder.java
package com.mkyong.testng.examples.suite;

import org.testng.annotations.Test;

public class TestOrder {

	@Test(groups={"orderBo", "save"})
public void testMakeOrder() {
System.out.println("testMakeOrder");
} @Test(groups={"orderBo", "save"})
public void testMakeEmptyOrder() {
System.out.println("testMakeEmptyOrder");
} @Test(groups="orderBo")
public void testUpdateOrder() {
System.out.println("testUpdateOrder");
} @Test(groups="orderBo")
public void testFindOrder() {
System.out.println("testFindOrder");
} }
 

2. Testng.xml

To run above test classes, create a XML file – testng.xml (can be any filename) file, and define detail like this :

testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="TestAll">

	<test name="order">
<classes>
<class name="com.mkyong.testng.examples.suite.TestConfig" />
<class name="com.mkyong.testng.examples.suite.TestOrder" />
</classes>
</test> <test name="database">
<classes>
<class name="com.mkyong.testng.examples.suite.TestConfig" />
<class name="com.mkyong.testng.examples.suite.TestDatabase" />
</classes>
</test> </suite>

Output

[TestNG] Running:
C:\mkyong_projects\TestNG\src\test\resources\testng-all.xml testBeforeSuite() testBeforeTest()
testFindOrder
testMakeEmptyOrder
testMakeOrder
testUpdateOrder
testAfterTest() testBeforeTest()
testConnectMongoDB
testConnectMsSQL
testConnectMySQL
testConnectOracle()
testAfterTest() testAfterSuite()
 

3. Other Examples

Here are some common use examples.

3.1 Specify package names instead of class names:

testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="TestAll">

	<test name="order">
<packages>
<package name="com.mkyong.testng.examples.suite.*" />
</packages>
</test> </suite>

3.2 Specify methods to include or exclude :

testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="TestAll">

  <test name="order">
<classes>
<class name="com.mkyong.testng.examples.suite.TestConfig" />
<class name="com.mkyong.testng.examples.suite.TestOrder">
<methods>
<include name="testMakeOrder" />
<include name="testUpdateOrder" />
<!--
<exclude name="testMakeOrder" />
-->
</methods>
</class>
</classes>
</test> </suite>

Output

[TestNG] Running:
C:\mkyong_projects\TestNG\src\test\resources\testng.xml testBeforeSuite()
testBeforeTest()
testMakeOrder
testUpdateOrder
testAfterTest()
testAfterSuite()

3.3 Specify groups to include or exclude :

testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="TestAll">

  <test name="database">
<groups>
<run>
<exclude name="brokenTests" />
<include name="db" />
</run>
</groups> <classes>
<class name="com.mkyong.testng.examples.suite.TestDatabase" />
</classes>
</test> </suite>

Output

[TestNG] Running:
C:\mkyong_projects\TestNG\src\test\resources\testng.xml testConnectMsSQL
testConnectOracle()

 

According to the TestNG dtd, the exclude element is only applicable to the following elements:

  • package - The package description within packages list.
  • methods - The list of methods to include/exclude from this test.
  • run - The subtag of groups used to define which groups should be run.

The elements cl*** and class cannot be directly excluded; however, you can exclude cl*** through groups:

@Test(groups = { "ClassTest1" })
public class Test1 { public void testMethod1() {
} public void testMethod2() {
} }

Then you will define the testng.xml:

<suite>
<test>
<groups>
<run>
<exclude name="ClassTest1"/>
</run>
</groups>
<cl***>
<class name="Test1">
</cl***>
</test>
</suite>

TestNG – Run multiple test classes (suite test)的更多相关文章

  1. PHPFarm - How to run multiple versions of PHP on the same computer

    How to Run Multiple Versions of PHP on One Server 转载:http://www.sitepoint.com/run-multiple-versions- ...

  2. 【转帖】如何在redhat单机服务器上运行postgresql的多个实例(howto run multiple postgresql instance on one redhat server)

    Running multiple PostgreSQL 9.2 Instances on one server in CentOS 6/RHEL 6/Fedora 原帖网站速度很慢,故转帖在此 Thi ...

  3. Run Multiple Webpack Configs Sequentially

    https://www.viget.com/articles/run-multiple-webpack-configs-sequentially/ const path = require('path ...

  4. Spark机器学习· 实时机器学习

    Spark机器学习 1 在线学习 模型随着接收的新消息,不断更新自己:而不是像离线训练一次次重新训练. 2 Spark Streaming 离散化流(DStream) 输入源:Akka actors. ...

  5. Spark机器学习9· 实时机器学习(scala with sbt)

    1 在线学习 模型随着接收的新消息,不断更新自己:而不是像离线训练一次次重新训练. 2 Spark Streaming 离散化流(DStream) 输入源:Akka actors.消息队列.Flume ...

  6. ExtentReports 结合 TestNg 生成自动化 html 报告 (支持多 suite)

    转载:https://testerhome.com/topics/8134 重要说明:报告监听器源码修复一些bug,不再此处更新代码,最新代码可以到github查看最新报告监听器源码 前几天分享了ht ...

  7. testng 执行多个suite

    我们知道testng的配置文件,一个.xml里面只能有一个suite,那么如果想要设置多个suite怎么弄呢?这个时候我们需要用到testng的标签<suite-files>. 下面说一下 ...

  8. testng执行多个suite

    由于testng.xml中只能设置一个<suite>标签,就无法创建多个测试集,通过<suite-files >标签可以实现允许多个测试集. 1.testng.xml中引入多个 ...

  9. TestNG Suite 运行出现中文乱码如何解决

    场景: 用TestNG框架运行测试类,控制台视图输出出现中文乱码. 解决方案: 1.eclipse属性>workspace>other>utf-8 2.修改eclipse.ini 文 ...

随机推荐

  1. iOS WKWebView

    Webkit 是 iOS 8.0 后提供的新的框架,组件WKWebView比较UIWebView 速度更快.占用内存更少了,可支持性更多 WKWebView可通过KVO监听属性 title.estim ...

  2. OSPF 提升 三 type of Areas

    ospf  ccnp 三 上图中 rip域中的不连续的100条路由   在a1中导致LSDB太大      在保证网段的可达性的前提下   尽可能减少区域内路由器的lsdb       可以将a1设置 ...

  3. POJ-2078 Matrix,暴力枚举!

                                                                 Matrix 题意:一个n*n的数字矩阵,每次操作可以对任意一行或者一列进行循 ...

  4. fdisk分区自动挂载

    理解/etc/fstab文件配置 首先打开这个文件我们查看下本身内容 vi /etc/fstab   或者   vim /etc/fstab 2 介绍下fstab配置 文件配置每一行属于一个配置,每个 ...

  5. Ignite集成Spark之IgniteDataFrames

    下面简要地回顾一下在第一篇文章中所谈到的内容. Ignite是一个分布式的内存数据库.缓存和处理平台,为事务型.分析型和流式负载而设计,在保证扩展性的前提下提供了内存级的性能. Spark是一个流式数 ...

  6. BZOJ 4318 OSU! ——期望DP

    这次要求$x^3$的概率和. 直接维护三个值$x$ $x^2$ $x^3$的期望. 概率的平方不等于平方的概率. #include <map> #include <ctime> ...

  7. css3鼠标点击穿透--摘抄

    有些时候网页中用到了一些绝对定位的Div,因为需要事先这个Div是隐藏的,但是它所在的位置会遮挡住鼠标点击事件.这个时候可以用CCS3中的pointer-events属性来解决.   //穿透该层 p ...

  8. R语言入门---杂记(一)---R的常用函数

    1.nchar():查看字符串长度. 2.rev(): 给你的数据翻个个 3.sort():给你数据排个序(默认从小到大依次排列) 4.runif():产生均匀分布的随机数 #runif

  9. yii 之删除数据

    public function actionTest(){ //删除 //方法一 $result = Test::find()->where(['id' => 1])->all(); ...

  10. Laravel 之Composer

    Composer是php的依赖管理工具,不是包管理器. 安装方式: (1)Composer-Setup.exe Win操作系统.需FQ (2)composer.phar 通用安装方式.不需要FQ,wi ...