转自:http://www.mkyong.com/unittest/testng-tutorial-7-dependency-test/

In TestNG, we use dependOnMethods and dependsOnGroups to implement dependency testing. If a dependent method is fail, all the subsequent test methods will be skipped, NOT failed.

1. dependOnMethods Example

A simple example, “method2()” is dependent on “method1()”.

1.1 If method1() is passed, method2() will be executed.

App.java
package com.mkyong.testng.examples.dependency;

import org.testng.annotations.Test;

public class App {

	@Test
public void method1() {
System.out.println("This is method 1");
} @Test(dependsOnMethods = { "method1" })
public void method2() {
System.out.println("This is method 2");
} }

Output

This is method 1
This is method 2
PASSED: method1
PASSED: method2 ===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================

1.2 If method1() is failed, method2() will be skipped.

App.java
package com.mkyong.testng.examples.dependency;

import org.testng.annotations.Test;

public class App {

	//This test will be failed.
@Test
public void method1() {
System.out.println("This is method 1");
throw new RuntimeException();
} @Test(dependsOnMethods = { "method1" })
public void method2() {
System.out.println("This is method 2");
} }

Output

This is method 1
FAILED: method1
java.lang.RuntimeException
at com.mkyong.testng.examples.dependency.App.method1(App.java:10)
//... SKIPPED: method2 ===============================================
Default test
Tests run: 2, Failures: 1, Skips: 1
===============================================

2. dependsOnGroups Example

Let create few test cases to demonstrate the mixed use of dependsOnMethods anddependsOnGroups. See comments for self-explanatory.

TestServer.java
package com.mkyong.testng.examples.dependency;

import org.testng.annotations.Test;

//all methods of this class are belong to "deploy" group.
@Test(groups="deploy")
public class TestServer { @Test
public void deployServer() {
System.out.println("Deploying Server...");
} //Run this if deployServer() is passed.
@Test(dependsOnMethods="deployServer")
public void deployBackUpServer() {
System.out.println("Deploying Backup Server...");
} }
TestDatabase.java
package com.mkyong.testng.examples.dependency;

import org.testng.annotations.Test;

public class TestDatabase {

	//belong to "db" group,
//Run if all methods from "deploy" group are passed.
@Test(groups="db", dependsOnGroups="deploy")
public void initDB() {
System.out.println("This is initDB()");
} //belong to "db" group,
//Run if "initDB" method is passed.
@Test(dependsOnMethods = { "initDB" }, groups="db")
public void testConnection() {
System.out.println("This is testConnection()");
} }
TestApp.java
package com.mkyong.testng.examples.dependency;

import org.testng.annotations.Test;

public class TestApp {

	//Run if all methods from "deploy" and "db" groups are passed.
@Test(dependsOnGroups={"deploy","db"})
public void method1() {
System.out.println("This is method 1");
//throw new RuntimeException();
} //Run if method1() is passed.
@Test(dependsOnMethods = { "method1" })
public void method2() {
System.out.println("This is method 2");
} }

Create a XML file and test them together.

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

<suite name="TestDependency">

  <test name="TestCase1">

	<classes>
<class
name="com.mkyong.testng.examples.dependency.TestApp">
</class>
<class
name="com.mkyong.testng.examples.dependency.TestDatabase">
</class>
<class
name="com.mkyong.testng.examples.dependency.TestServer">
</class>
</classes> </test> </suite>

Output

Deploying Server...
Deploying Backup Server...
This is initDB()
This is testConnection()
This is method 1
This is method 2 ===============================================
TestDependency
Total tests run: 6, Failures: 0, Skips: 0
===============================================

TestNG – Dependency Test的更多相关文章

  1. Maven 配置 Selenium + testNG + reportNG 运行环境

    .markdown-preview:not([data-use-github-style]) { padding: 2em; font-size: 1.2em; color: rgb(56, 58, ...

  2. Mac Eclipse+Maven+TestNg+ReportNg 生成测试报告

    TestNG 是java 的单元测试框架,功能很强大,很方便,但是自动生成的测试报告有待改善,可以使用TestNg 自带的TestNG_xslt更改TestNG报告的样式,这里主要讲解ReportNg ...

  3. Webdriver+testNG+ReportNG+Maven+SVN+Jenkins自动化测试框架的pom.xml配置

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  4. 【搬运工】之——Selenium+IDEA+Maven+TestNG环境搭建(转)

    Selenium+IDEA+Maven+TestNG环境搭建 第一 安装java环境. 1. 下载并安装Jdk1.7或Jdk1.8 http://www.oracle.com/technetwork/ ...

  5. Selenium+IDEA+Maven+TestNG环境搭建

    第一 安装java环境. 1. 下载并安装Jdk1.7或Jdk1.8 http://www.oracle.com/technetwork/java/javase/downloads/index.htm ...

  6. Java + Selenium + TestNG + Maven

    环境准备: 1. Java: Install Java jdk: Version: java 1.8 or aboveConfigure Java Environment Variables:Add ...

  7. 接口自动化:HttpClient + TestNG + Java(一) - 接口测试概述+自动化环境搭建

    1.1 接口测试简介 1.1.1 什么是接口测试 开始学习接口自动化测试之前,我们先要来了解什么是接口,以及什么是接口测试. 我们都知道,测试从级别上划分可以分为 组件测试 集成测试 系统测试 验收测 ...

  8. selenium java maven testNg环境搭建

    maven获取jar的xml地址:http://mvnrepository.com 步骤一安装jdk(略) 步骤二 安装eclipse(略) 步骤三 安装testNG 步骤四 maven安装 步骤三 ...

  9. Maven捆绑TestNG实现测试自动化执行、部署和调度

    一. 需求介绍 自动化测试,尤其是接口测试时,要写大量的测试用例,这些测试用例我们当然首选使用TesteNG编写,用例数量大,还涉及各种依赖包之类的问题,因此用Maven管理也是最方便最易实现的. 面 ...

随机推荐

  1. SQL server 数据库备份至服务器本地磁盘和其他服务器磁盘

    前言 因工作需要将数据库每日备份至服务器本地磁盘和其他服务器磁盘,对网上的例子进行改造,解决了SQL server数据库备份的问题. 一.将指定数据库备份到本地 将数据库affair_stydy全备份 ...

  2. mac osx下apache下的坑: you don’t have permission to access / on this server

    在Mac下Apache修改默认站点的目录时,遇到403错误, you don’t have permission to access / on this server 首先按照google到教程: 修 ...

  3. Repeat Array Generator & String.repeat

    Repeat Array Generator RepeatArrayGenerator "use strict"; /** * * @author xgqfrms * @licen ...

  4. iOS学习笔记16-数据库SQLite

    一.数据库 在项目开发中,通常都需要对数据进行离线缓存的处理,如新闻数据的离线缓存等.离线缓存一般都是把数据保存到项目的沙盒中.有以下几种方式: 归档:NSKeyedArchiver 偏好设置:NSU ...

  5. bzoj 4401 块的计数 思想+模拟+贪心

    块的计数 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 455  Solved: 261[Submit][Status][Discuss] Descr ...

  6. Spoj-BIPCSMR16 Team Building

    To make competitive programmers of BUBT, authority decide to take regular programming contest. To ma ...

  7. 【前端学习笔记】2015-09-11~~~~ js中ajax请求返回案例

    <body><textarea id='a' rows=100 cols=300>result:</textarea>><script>var a ...

  8. Android 之 下拉框(Spinner)的使用-转

    下拉列表 Spinner. Spinner的使用,可以极大提高用户的体验性.当需要用户选择的时候,可以提供一个下拉列表将所有可选的项列出来.供用户选择. Demo如下,可以留作参考 一.使用数组作为数 ...

  9. compose.yml模板文件

    默认的模板文件名称为 docker-compose.yml,格式为 YAML 格式. 示例: version: " services: webapp: image: examples/web ...

  10. golang并发编程goroutine+channel(一)

    go语言的设计初衷除了在不影响程序性能的情况下减少复杂度,另一个目的是在当今互联网大量运算下,如何让程序的并发性能和代码可读性达到极致.go语言的并发关键词 "go" go dos ...