转自: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. vim第五章 命令行模式

    vim第五章命令行模式 技巧 27 结识vim的命令行模式 在命令行模式中执行的命令有被称作ex命令    在按/调出查找提示符或者<C-r>=访问表示寄存器时 命令行模式也被激活     ...

  2. zoj 2388 Beat the Spread!

    Beat the Spread! Time Limit: 2 Seconds      Memory Limit: 65536 KB Superbowl Sunday is nearly here. ...

  3. P2341 受欢迎的牛

    受欢迎的牛 洛谷链接 题目大意: 有n头牛,牛会喜欢其他牛和自己,而喜欢是可以传递的,求被所有牛喜欢的牛的数量. 思路: 这是一道tarjan缩点的题目.被所有牛都喜欢的牛,一定会在一个强连通分量里. ...

  4. 【Luogu】P1122最大子树和(DFS,树上DP)

    题目链接 感觉自己DP好烂啊   道道看题解 钦定1为根,DFS搜索子树权值.如果子树权值大于0就将当前节点加上子树权值,反之就把子树扔掉.最后在所有节点的权值中寻找最优解. void dfs(int ...

  5. POJ 2914 Minimum Cut 全局最小割

    裸的全局最小割了吧 有重边,用邻接矩阵的时候要小心 #include<iostream> #include<cstdio> #include<bitset> #in ...

  6. BestCoder Round #36

    HDU5198 Strange Class 问题描述 在Vivid的学校里,有一个奇怪的班级(SC).在SC里,这些学生的名字非常奇怪.他们的名字形式是这样的anbncn(a,b,c两两不相同.).例 ...

  7. LA 并查集路径压缩

    题目大意:有n个节点,初始时每个节点的父亲节点都不存在.有两种操作 I u v:把点节点u的父亲节点设为v,距离为|u-v|除以1000的余数.输入保证执行指令前u没有父亲节点. E u:询问u到根节 ...

  8. ElasticSearch索引自定义类型

    ES可以自动检测字段并设置映射类型.如果设置的索引类型不是我们所需要的,我们可以自行定义. Rest API设置自定义索引 首先通过ES自动映射一个IP地址的字段的类型: <pre name=& ...

  9. Redis命令行之String

    一.Redis之String简介 1. String是redis最基本的数据类型,一个key对应一个value. 2. String是二进制安全的,可以包含任何数据,例如图片或序列化的对象. 3. S ...

  10. java对接网银支付案例

    =================提交表单jsp,http://www.cnblogs.com/qgc88================================ <%@page con ...