简单介绍

运行TestNG测试脚本有两种方式:一种是直接通过IDE运行(例如使用eclipse中的“Run TestNG tests”),另一种是从命令行运行(通过使用xml配置文件)。当我们想执行某个包或者某个类中的一部分测试脚本的时候,使用xml配置文件是非常便利的。在xml配置文件里,不仅可以选择某些需要执行的测试脚本,还可以排除某些不需要运行的测试脚本。

创建testng.xml文件

创建xml文件很容易,只需要在其中填充一些内容。

1)首先要声明一个suite的名字,用于描述将要运行的测试脚本集,可以根据自己需要任意命名,最终这个名字会在testng的测试报告中看到。

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="SuiteName" verbose="1" >
<test name="TestName" >

2)如果选择的测试脚本是基于组的(使用了@Test (groups={"student"})这样的注解),那么接下来需要声明如何使用这些组:包含或者排除。如果使用了include标签标注某些组,那么在选择的测试脚本中,只有属于那些组的测试脚本会被运行。那些未被选中的测试脚本,或者被选中却不属于某些组的测试脚本都不会被运行。需要注意,一个测试脚本可以属于很多个组,只要有一个组被include标签标注,那么它就会被运行。如果使用了exclude标签标注某些组,那么在选择的脚本中,只有不属于那些组的测试脚本会被运行。如果同时使用了include标签和exclude标签,那么拥有被include标注的组的那些脚本会被运行,拥有被exclude标注的脚本不会被运行。有一个例外是,一个组同时被include和exclude标注了,那么拥有这个组的脚本会被运行。include和exclude标签的使用方式如下:

<groups>
<run>
<include name = "includedGroupName" />
<exclude name = "excludedGroupName" />
</run>
</groups>

3)选择测试脚本可以从包、类、方法三个层级进行。

选择一个包

<packages>
<package name = "packageName" />
</packages>

选择一个类

<classes>
<class name = "className" />
</classes>

选择一个方法

<classes>
<class name = "className" />
<methods>
<include name = "methodName" />
</methods>
</class>
</classes>

xml文件样例

1)选择一个包中的全部测试脚本(包含子包)

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="First suite" verbose="1" >
<test name = "allTestsInAPackage" >
<packages>
<package name = "whole.path.to.package.* />
</packages>
</test>
</suite>

2)选择一个类中的全部测试脚本

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Second suite" verbose="1" >
<test name = "allTestsInAClass" >
<classes>
<class name="whole.path.to.package.className />
</classes>
</test>
</suite>

3)选择一个类中的部分测试脚本

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Third suite" verbose="1" >
<test name = "aFewTestsFromAClass" >
<classes>
<class name="whole.path.to.package.className >
<methods>
<include name = "firstMethod" />
<include name = "secondMethod" />
<include name = "thirdMethod" />
</methods>
</class>
</classes>
</test>
</suite>

4)选择一个包中的某些组

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Fourth suite" verbose="1" >
<test name = "includedGroupsInAPackage" >
<groups>
<run>
<include name = "includedGroup" />
</run>
</groups>
<packages>
<package name = "whole.path.to.package.* />
</packages>
</test>
</suite>

5)排除一个包中的某些组

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Fifth suite" verbose="1" >
<test name = "excludedGroupsInAPackage" >
<groups>
<run>
<exclude name = "excludedGroup" />
</run>
</groups>
<packages>
<package name = "whole.path.to.package.* />
</packages>
</test>
</suite>

在maven的pom.xml文件中配置testng.xml

需要在pom文件中,指明testng.xml文件的位置。

maven使用surefire这个插件进行测试,可以执行testng或者Junit脚本。

语法为 <suiteXmlFile>src/test/resources/testNGFilesFolder/${testNgFileName}.xml</suiteXmlFile>

    <dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>//该文件位于工程根目录时,直接填写名字,其它位置要加上路径。
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>

运行测试脚本

方法一:在IDE,例如IntellJ IDEA中,鼠标右击testng.xml文件,选择run即可。

方法二:进入到项目工程的根目录,使用 mvn clean test -Dtestng.xml 命令,结果如下:

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building ArtifactIdWang 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
.
.
.
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
now start test
Test started runningtemplistener1at:
i'm listenerTest1
Result success
Test started runningtemplistener2at:
i'm listenerTest2
Result success
now finish test
Tests run: , Failures: , Errors: , Skipped: , Time elapsed: 7.125 sec - in TestSuite Results : Tests run: , Failures: , Errors: , Skipped: [INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: : min
[INFO] Finished at: --15T10::+:
[INFO] Final Memory: 22M/182M
[INFO] ------------------------------------------------------------------------

testng xml配置文件的更多相关文章

  1. 【TestNG测试】TestNG、Maven、testng.xml构建测试工程

    创建一个maven工程 使用Idea创建maven工程 ​ 建立类似如上的工程结构,src/main/java,src/test/java,pom.xml,testng.xml,这里由于我们使用工程是 ...

  2. TestNG的testng.xml配置概述

    TestNG提供的annotaions用来辅助定义测试类. TestNG的testng.xml配置文件用来辅助定义执行什么样的测试,即testng.xml更像是一个测试规划. testng.xml配置 ...

  3. testng.xml文件的配置

    ------Web自动化测试之Webdriver+TestNG--从零到熟练(系列) TestNG用来管理测试用例的是testng.xml配置文件,我们可以通过配置这个文件来达到组织测试用例,输出测试 ...

  4. testng教程之testng.xml的配置和使用,以及参数传递

    昨天学习了一下testng基础教程,http://www.cnblogs.com/tobecrazy/p/4579414.html 昨天主要学习的是testng 的annotation基本用法和生命周 ...

  5. IDEA 单元测试testng入门及testng.xml

    直接进入正题: 1.TestNG的运行方式如下: With a testng.xml file 直接run as test suite With ant 使用ant From the command ...

  6. 接口自动化框架(java)--5.通过testng.xml生成extentreport测试报告

    这套框架的报告是自己封装的 由于之前已经通过Extentreport插件实现了Testng的IReport接口,所以在testng.xml中使用listener标签并指向实现IReport接口的那个类 ...

  7. testng.xml 配置大全

    1.TestNG的运行方式如下: 1 With a testng.xml file 直接run as test suite 2 With ant 使用ant 3 From the command li ...

  8. 创建testng.xml文件

    简单介绍 运行TestNG测试脚本有两种方式:一种是直接通过IDE运行(例如使用eclipse中的“Run TestNG tests”),另一种是从命令行运行(通过使用xml配置文件).当我们想执行某 ...

  9. Java - Test - TestNG: testng.xml 元素 group

    1. 概述 group 相关的元素 groups run 其他相关(不准备提了) package class include exclude 2. 背景 准备 了解了 class 及其子元素 问题 对 ...

随机推荐

  1. 【ATX学习大纲】【ATX基于uiautomator2+Python学习】之Android自动化

    github学习地址:https://github.com/openatx/uiautomator2 <_io.TextIOWrapper name='<stderr>' mode= ...

  2. C#反射Assembly 详细说明,有项目例子

    1.对C#反射机制的理解2.概念理解后,必须找到方法去完成,给出管理的主要语法3.最终给出实用的例子,反射出来dll中的方法 反射是一个程序集发现及运行的过程,通过反射可以得到*.exe或*.dll等 ...

  3. 【BZOJ】3432: [Usaco2014 Jan]Cross Country Skiing (bfs+二分)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3432 题目说要相互可达,但是只需要从某个点做bfs然后判断其它点是否可达即可. 原因太简单了.... ...

  4. (转)memcache、redis缓存

    memcache原理.内存模型: http://www.csdn.net/article/2016-03-16/2826609 redis原理: http://baike.baidu.com/link ...

  5. 【Raspberry Pi】定时运行python程序读温湿度传感器数据&发邮件

    1.定时执行脚本 http://tech.it168.com/a2011/0707/1214/000001214830_all.shtml /sbin/service crond start //启动 ...

  6. reactjs中props和state最佳实践

    http://blog.csdn.net/dangnian/article/details/50998981

  7. pandas基础: Series和DataFrame的简单介绍

    一.pandas 是什么 pandas 是基于 NumPy 的一个 Python 数据分析包,主要目的是为了数据分析.它提供了大量高级的数据结构和对数据处理的方法. pandas 有两个主要的数据结构 ...

  8. TypeScript 函数 (五)

    传递给一个函数的参数个数必须与函数期望的参数个数一致. 参数类别: 必须参数 可选参数 :可选参数必须在参数后面. 默认参数 :当用户没有传递这个参数或传递的值是undefined时. 它们叫做有默认 ...

  9. Ruby on Rails 初次冲浪体验

    为了更好的阅读体验,欢迎訪问 作者博客原文 Rails is a web application development framework written in the Ruby language. ...

  10. 【BZOJ4711】小奇挖矿 树形DP

    [BZOJ4711]小奇挖矿 Description [题目背景] 小奇在喵星系使用了无限非概率驱动的采矿机,以至于在所有星球上都采出了一些矿石,现在它准备建一些矿石仓库并把矿石运到各个仓库里. [问 ...