TestNG依賴測試


有時,我們可能需要以特定順序呼叫測試用例中的方法,或者可能希望在方法之間共用一些資料和狀態。 TestNG支援這種依賴關係,因為它支援在測試方法之間顯式依賴的宣告。

TestNG允許指定依賴關係:

  • @Test注釋中使用屬性dependsOnMethods,或者
  • @Test注釋中使用屬性dependsOnGroups

在TestNG中,我們使用dependOnMethodsdependsOnGroups來實現依賴測試。 如果依賴方法失敗,則將跳過所有後續測試方法。

為了方便演示使用,首先建立一個 Maven 專案: DependOnTest,其專案結構如下所示 -

1. dependOnMethods範例

一個簡單的例子,「method2()」依賴於「method1()」。

1.1. 如果method1()通過,那麼將執行method2()

建立一個Java檔案: App.java,其程式碼結構如下所示 -

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");
    }

}

執行上面程式碼,得到以下結果 -

[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

1.2. 如果method1()失敗,則將跳過method2()

建立一個Java檔案: App2.java,其程式碼結構如下所示 -

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");
    }

}

執行上面程式碼,得到以下結果 -

[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

2. dependsOnGroups範例

下面我們建立幾個測試用例來證明dependsOnMethodsdependsOnGroups的混合使用。 程式碼比較簡單,參閱下面註釋就能明白了。

建立一個Java檔案: TestServer.java,其程式碼結構如下所示 -

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...");
    }

}

建立一個Java檔案: TestDatabase.java,其程式碼結構如下所示 -

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()");
    }

}

建立一個Java檔案: TestApp.java,其程式碼結構如下所示 -

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");
    }

}

建立一個XML檔案: testng.xml,其程式碼結構如下所示 -

<?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>

執行上面程式碼,得到以下結果 -

[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
===============================================

執行過程和時間如下 -