转自: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. hdu2074

    我先求出交叉的gird,然后再一行一行求得.感觉还可以吧.思路比较清晰,开始想的是数是第几行然后从每一行的前后开始控制,好麻烦的感觉,我就先求出来了框架再做就好做多啦!后来PE,突然发现我特殊处理n= ...

  2. 【Luogu】P2324骑士精神(IDA*)

    题目链接 当guess>limit-deep的时候return就好了. guess是估价函数,值为不在自己地盘上的骑士个数.limit是本次迭代阈值.deep是已经走了多少步. 这个优化是显然的 ...

  3. [BZOJ1604] [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(好题)

    传送门 良心题解 #include <set> #include <cstdio> #include <iostream> #include <algorit ...

  4. 转载:sql2005 Microsoft SQL Server Management Studio Express的安装问题

    转载地址:http://blog.csdn.net/rjc20098022/article/details/26958105 在这个网址http://www.microsoft.com/zh-cn/d ...

  5. 2013年EI收录的中国期刊

    ISSN 刊名 0567-7718 Acta Mechanica Sinica 1006-7191 Acta Metallurgica Sinica (English Letters) 0253-48 ...

  6. 创建mavenweb项目

    1.创建maven项目 2.创建maven-web项目 3.将webapp文件夹复制到maven项目下,src路径下

  7. 【java小工具】从.java文件中筛选出方法,并计算出方法在这些文件中出现的次数

    package getMethod; import java.io.*; import java.util.*; import java.util.regex.Matcher; import java ...

  8. uva 11178二维几何(点与直线、点积叉积)

    Problem D Morley’s Theorem Input: Standard Input Output: Standard Output Morley’s theorem states tha ...

  9. tensorflow加载embedding模型进行可视化

    1.功能 采用python的gensim模块训练的word2vec模型,然后采用tensorflow读取模型可视化embedding向量 ps:采用C++版本训练的w2v模型,python的gensi ...

  10. Yii 之数据库查询

    模型代码: <?php namespace app\models; use yii\db\ActiveRecord; class Test extends ActiveRecord{ } 控制器 ...