这一篇我们主要学习如下几个知识点:

⑴关于testng.xml

⑵创建一个测试套件

⑶执行testng.xml

⑷在测试套件中创建多个测试用例

⑸在用例中增加class,packages, method

⑹用例中包含/不包含class,packages, method

⑺⑻⑼⑽

在开始学习这些知识点之前,需要在eclipse中先创建一个java 项目,结构如下:

在每个class文件中增加如下代码

import org.testng.annotations.Test;

public class FirstTestClass {
@Test
public void firstTest(){
System.out.println("First test method");
}

@Test
public void secondTest(){
System.out.println("Second test method");
}

}

其中方法test.thirdpackage.FirstTestClass.firstTest()的注解修改为 @Test (groups={"test-one"}) 【关注testng的注解在后面的文章中会有解释】

1、class-testng.xml 文件用来展示如何只执行class,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Class Suite" verbose="1">
<test name="Test">
<classes>
<class name="test.firstpackage.FirstTestClass"/>
<class name="test.secondpackage.FirstTestClass"/>
<class name="test.thirdpackage.FirstTestClass"/>

</classes>
</test>
</suite>

2、combine-testng.xml文件用来展示如何执行firstpackage,secondpackage.FirstTestClass,thirdpackage.FirstTestClass.firstTest,内容如下

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Combine Suite" verbose="1">
<test name="Combine Test">
<packages>
<package name="test.firstpackage" />
</packages>
<classes>
<class name="test.secondpackage.FirstTestClass" />
<class name="test.thirdpackage.FirstTestClass" >
<methods>
<include name="firstTest"/>
</methods>
</class>
</classes>
</test>
</suite>

3、exclude-method-testng.xml用来展示如何不包含methods,内容如下;

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Exclude Method Suite" verbose="1">
<test name="Exclude Method Test">
<classes>
<class name="test.firstpackage.FirstTestClass">
<methods>
<exclude name="firstTest" />
</methods>
</class>
</classes>
</test>
</suite>

4、exclude-package-testng.xml用例展示如何不执行某个packages,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Exclude Method Suite" verbose="1">
<test name="Exclude Method Test">
<classes>
<class name="test.firstpackage.FirstTestClass">
<methods>
<exclude name="firstTest" />
</methods>
</class>
</classes>
</test>
</suite>

5、include-package-testng.xml用例展示如何只执行packages,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Include Package Suite" verbose="1">
<test name="Include Package Test">
<packages>
<package name="test.*">
<include name="test.secondpackage"/>
</package>
</packages>
</test>
</suite>

6、method-testng.xml用例展示如何只执行某个方法

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Method Suite" verbose="1">
<test name="Method Test">
<classes>
<class name="test.firstpackage.FirstTestClass">
<methods>
<include name="firstTest"/>
</methods>
</class> 
<!--  <class name="test.thirdpackage.FirstTestClass"/> -->

</classes>
</test>
</suite>

7、group-testng.xml 展示了如何执行某个组中的用例

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Group Suite" verbose="1">
<test name="Group Test">
<groups>
<run>
<include name="test-one" />
</run>
</groups>
<classes>
<class name="test.thirdpackage.FirstTestClass" />
</classes>
</test>
</suite>

准备好之后,右击xml文件,【Run As】——【TestNG Suite】然后在 eclipse底部的【Result of running suite】中查看每个xml执行的结果吧

TestNG 入门指导——理解testng.xml执行/不执行某个包,某个类,某个方法的更多相关文章

  1. 执行引入外部 jar 包的类的方法

    liunx 系统中,命令行中语法:(.后面是冒号:) java -cp .:third.jar MyClass windows 系统中命令行的语法:(.后面是分号;) java -cp .;third ...

  2. testng入门教程12 TestNG执行多线程测试

    testng入门教程 TestNG执行多线程测试 testng入门教程 TestNG执行多线程测试 并行(多线程)技术在软件术语里被定义为软件.操作系统或者程序可以并行地执行另外一段程序中多个部分或者 ...

  3. testng入门教程10 TestNG参数化测试

    在TestNG的另一个有趣的功能是参数测试.在大多数情况下,你会遇到这样一个场景,业务逻辑需要一个巨大的不同数量的测试.参数测试,允许开发人员运行同样的测试,一遍又一遍使用不同的值. TestNG让你 ...

  4. testng入门教程9 TestNG依赖测试

    有时候,你可能需要在一个特定的顺序调用方法在测试案例,或你想分享一些数据和方法之间的状态.TestNG支持这种依赖测试方法之间的显式依赖它支持声明. TestNG允许指定依赖,无论与否: 使用属性de ...

  5. testng入门教程8 TestNG异常测试

    TestNG跟踪异常处理代码提供了一个选项.可以测试是否需要代码抛出异常或不抛出. @Test注释expectedExceptions 参数一起使用.现在,让我们来看看@Test(expectedEx ...

  6. testng入门教程7 TestNG组测试

    在TestNG中组测试是一个新的创新功能,它不存在于JUnit框架,它允许调度到适当的部分方法和瓶坯复杂的测试方法分组.您不仅可以声明属于群体的那些方法,但你也可以指定一组包含其他组.然后,TestN ...

  7. testng入门教程6 TestNG忽略测试

    有时,我们的代码是没有准备好,如果测试用例写入到测试方法/代码将无法运行,在这种情况下,@Test(enabled = false)有助于禁用此测试案例. 测试方法是标注了@Test(enabled ...

  8. testng入门教程11 TestNG运行JUnit测试

    现在,您已经了解了TestNG和它的各种测试,如果现在担心如何重构现有的JUnit代码,那就没有必要,使用TestNG提供了一种方法,从JUnit和TestNG按照自己的节奏.也可以使用TestNG执 ...

  9. jvm入门及理解(三)——运行时数据区(程序计数器+本地方法栈)

    一.内存与线程 内存: 内存是非常重要的系统资源,是硬盘和cpu的中间仓库及桥梁,承载着操作系统和应用程序的实时运行.JVM内存布局规定了JAVA在运行过程中内存申请.分配.管理的策略,保证了JVM的 ...

随机推荐

  1. VS2010如何重置开发环境

    在利用VS进行软件开发的过程中,我们时不时要因为各种原因,对VS的开发环境进行变动,对于很多初次接触VS这样一个十分好用方便的编程工具的人来说,更改编程环境成了一个难题,今天我们就来讲解一下,如何更改 ...

  2. Linux学习笔记(九)

    <span style="font-size:18px;">本人使用的是centos 因为用使用些特殊命令.不得不改动系统的时间这里总结例如以下: date 查看系统时 ...

  3. lua笔记二 赋值语句

    赋值是改变一个变量的值和改变表域的最基本的方法. a = "hello" .. "world" t.n = t.n + 1 Lua可以对多个变量同时赋值,变量列 ...

  4. prometheus-dashboard-to-grafana

    https://prometheus.io/docs/visualization/grafana/ https://www.digitalocean.com/community/tutorials/h ...

  5. SolrCloud基本过程

    转:http://www.data321.com/yunjisuan/20160514880/SolrZhiJieDuQuZKZhongDePeiZhiXin SolrCloud之分布式索引及与Zoo ...

  6. MediaType是application/x-www-form-urlencoded的接口测试方法

    先看接口: @POST @Path("/deleteById") //@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaTy ...

  7. 【Ubuntu】VirtualBox 您没有查看“sf_VirtualDisk”的内容所需的权限。

    转自:https://www.cnblogs.com/laishenghao/p/5346651.html 最终解决办法: sudo adduser lqr vboxsf 这里lqr是我的用户名 然后 ...

  8. Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

    System.Data.ConstraintException: Failed to enable constraints. One or more rows contain values viola ...

  9. JAVA-JSP内置对象之exception对象用来处理错误异常

    相关资料:<21天学通Java Web开发> exception对象1.exception对象用来处理错误异常.2.如果要使用exception对象,必须指定page中的isErrorPa ...

  10. Writing your first Django

    Quick install guide 1.1   Install Python, it works with Python2.6, 2.7, 3.2, 3.3. All these version ...