有時,我們可能需要以特定順序調(diào)用測試用例中的方法,或者可能希望在方法之間共享一些數(shù)據(jù)和狀態(tài)。 TestNG支持這種依賴關(guān)系,因為它支持在測試方法之間顯式依賴的聲明。
TestNG允許指定依賴關(guān)系:
在TestNG中,我們使用dependOnMethods
和dependsOnGroups
來實現(xiàn)依賴測試。 如果依賴方法失敗,則將跳過所有后續(xù)測試方法。
為了方便演示使用,首先創(chuàng)建一個 Maven 項目: DependOnTest,其項目結(jié)構(gòu)如下所示 -
一個簡單的例子,“method2()
”依賴于“method1()
”。
method1()
通過,那么將執(zhí)行method2()
。創(chuàng)建一個Java文件: App.java,其代碼結(jié)構(gòu)如下所示 -
package com.yiibai;
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");
}
}
執(zhí)行上面代碼,得到以下結(jié)果 -
[TestNG] Running:
C:\Users\Administrator\AppData\Local\Temp\testng-eclipse-1021352376\testng-customsuite.xml
This is method 1
This is method 2
PASSED: method1
PASSED: method2
===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.XMLReporter@1b40d5f0: 91 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@6ea6d14e: 117 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@4563e9ab: 7 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 1 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@2aaf7cc2: 150 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@45c8e616: 3 ms
method1()
失敗,則將跳過method2()
。創(chuàng)建一個Java文件: App2.java,其代碼結(jié)構(gòu)如下所示 -
package com.yiibai;
import org.testng.annotations.Test;
public class App2 {
// 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");
}
}
執(zhí)行上面代碼,得到以下結(jié)果 -
[TestNG] Running:
C:\Users\Administrator\AppData\Local\Temp\testng-eclipse--1314180717\testng-customsuite.xml
This is method 1
FAILED: method1
java.lang.RuntimeException
at com.yiibai.App2.method1(App2.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
SKIPPED: method2
===============================================
Default test
Tests run: 2, Failures: 1, Skips: 1
===============================================
===============================================
Default suite
Total tests run: 2, Failures: 1, Skips: 1
===============================================
[TestNG] Time taken by org.testng.reporters.XMLReporter@1b40d5f0: 13 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@6ea6d14e: 63 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@4563e9ab: 17 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 72 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@2aaf7cc2: 72 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@45c8e616: 5 ms
下面我們創(chuàng)建幾個測試用例來證明dependsOnMethods
和dependsOnGroups
的混合使用。 代碼比較簡單,參閱下面注釋就能明白了。
創(chuàng)建一個Java文件: TestServer.java,其代碼結(jié)構(gòu)如下所示 -
package com.yiibai;
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...");
}
}
創(chuàng)建一個Java文件: TestDatabase.java,其代碼結(jié)構(gòu)如下所示 -
package com.yiibai;
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()");
}
}
創(chuàng)建一個Java文件: TestApp.java,其代碼結(jié)構(gòu)如下所示 -
package com.yiibai;
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");
}
}
創(chuàng)建一個XML文件: testng.xml,其代碼結(jié)構(gòu)如下所示 -
<?xml version="1.0" encoding="UTF-8"?>
<suite name="TestDependency">
<test name="TestCase1">
<classes>
<class
name="com.yiibai.TestApp">
</class>
<class
name="com.yiibai.TestDatabase">
</class>
<class
name="com.yiibai.TestServer">
</class>
</classes>
</test>
</suite>
執(zhí)行上面代碼,得到以下結(jié)果 -
[TestNG] Running:
F:\worksp\testng\DependOnTest\src\main\java\com\yiibai\testng.xml
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
===============================================
執(zhí)行過程和時間如下 -