Skip一个Test是一个自动化测试中非常常见的情形,TestNG官方文档只提到了什么情况下TestNG会自动Skip Test,但是没有提到如何主动的去Skip一个Test,下面是官方文档中的相关信息
4 - Running TestNG
-configfailurepolicy skip|continue Whether TestNG should continue to execute the remaining tests in the suite or skip them if an @Before* method fails. Default behavior is skip.5.7.1 - Dependencies with annotations
Hard dependencies. All the methods you depend on must have run and succeeded for you to run. If at least one failure occurred in your dependencies, you will not be invoked and marked as a SKIP in the report.
那么如何Skip一个Test呢,需要用到一个TestNG类SkipException,只要在代码中抛出这个异常,这次测试就会被标记为Skip,如果用到DataProvider,单条测试数据的运行也会被标成Skip,并不影响别的测试数据的运行。
下面是一个简单的example.
MyTester.java
“`
package com.demo.autotest.Executor;
import java.lang.Exception;
import org.testng.annotations.*;
import org.testng.SkipException;
/**
* @ClassName: MyTester
* @Description: Demo the SkipException
* @author: qinjun
*/
public class MyTester {
/**
* @Title: runTest1
* @Description:Run the test and throw SkipException
* @return: void
*/
@Test
public void runTest1() {
System.out.println("Start to throw the SkipException");
throw new SkipException("skip the test");
}
/**
* @Title: runTest1
* @Description:Run the test and throw Exception
* @return: void
*/
@Test
public void runTest2() {
System.out.println("Start to throw the Exception");
assert false:"fail the test";
}
}
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="suite1" verbose="2">
<parameter name="test_id" value="2"/>
<test name="test1">
<classes>
<class name="com.demo.autotest.Executor.MyTester" />
</classes>
</test>
</suite>
运行结果
…
… TestNG 6.9.4 by Cédric Beust (cedric@beust.com)
…[TestNG] Running:
E:\workspaces\DemoTestNGDataParam\src\main\java\com\demo\autotest\Executor\testng.xmlStart to throw the SkipException
Start to throw the Exception
FAILED: runTest2
java.lang.AssertionError: fail the test
at com.demo.autotest.Executor.MyTester.runTest2(MyTester.java:32)
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:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:659)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:845)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1153)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:771)
at org.testng.TestRunner.run(TestRunner.java:621)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:357)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:310)
at org.testng.SuiteRunner.run(SuiteRunner.java:259)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1199)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1124)
at org.testng.TestNG.run(TestNG.java:1032)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)SKIPPED: runTest1
org.testng.SkipException: skip the test
at com.demo.autotest.Executor.MyTester.runTest1(MyTester.java:26)
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:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:659)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:845)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1153)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:771)
at org.testng.TestRunner.run(TestRunner.java:621)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:357)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:310)
at org.testng.SuiteRunner.run(SuiteRunner.java:259)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1199)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1124)
at org.testng.TestNG.run(TestNG.java:1032)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)===============================================
test1Tests run: 2, Failures: 1, Skips: 1
===============================================
suite1Total tests run: 2, Failures: 1, Skips: 1
[TestNG] Time taken by org.testng.reporters.XMLReporter@3b22cdd0: 19 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@123772c4: 24 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@73a28541: 52 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@512ddf17: 11 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@4aa8f0b4: 6 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 5 ms
可以看出一个Fail,一个Skip。