23
2021
03

TestNG依赖测试

有时,我们可能需要以特定顺序调用测试用例中的方法,或者可能希望在方法之间共享一些数据和状态。 TestNG支持这种依赖关系,因为它支持在测试方法之间显式依赖的声明。


TestNG允许指定依赖关系:


在@Test注释中使用属性dependsOnMethods,或者

在@Test注释中使用属性dependsOnGroups。

在TestNG中,我们使用dependOnMethods和dependsOnGroups来实现依赖测试。 如果依赖方法失败,则将跳过所有后续测试方法。


为了方便演示使用,首先创建一个 Maven 项目: DependOnTest,其项目结构如下所示 -

287170544_57656.png

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

执行上面代码,得到以下结果 -

[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


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。