TestNG – Dependency Test
2024-08-27 08:38:31
转自: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
===============================================
最新文章
- 出操队形(LIS)
- UVa #11582 Colossal Fibonacci Numbers!
- IC卡复位应答ATR的数据元和它们的意义
- mui适用场景说明,能不能在普通浏览器里使用,能否用于wap网站
- var关键字详解
- jQuery的XX如何实现?——2.show与链式调用
- jquery去掉或者替换字符,设置指定options为selected状态
- winform学习之----进程和线程
- Ubuntu 学习笔记
- [转贴]Eclipse IDE for c++配置
- Highcharts下载与使用_数据报表图
- php正则验证手机号码
- TensorFlow学习笔记1
- jquery 获取表单的用户输入值的方法
- 绕过用编码方式阻止XSS攻击的几个例子
- C#打印图片
- information_schema系列六(索引,表空间,权限,约束相关表)
- String直接赋值和使用new的区别
- day9 python学习 文件的操作 读 写 seek
- mysql 伪列
热门文章
- 4A. Just a Hook
- A Survey of Model Compression and Acceleration for Deep Neural Network时s
- [adb 命令学习篇] adb 命令总结
- 【JavaScript 7—基础知识点】:BOM
- javascript前端下载
- Android从Fragment跳转到Activity
- NYOJ——239月老的难题(二分图最大匹配)
- 洛谷3830 [SHOI2012]随机树 【概率dp】
- 洛谷 [P2575] 高手过招
- Java远程调用BPS流程实现流程运行简单示例