TestNg依靠先进的采用强制的依赖,并依赖序列的------TestNg依赖于特定的解释(两)

原创文章,版权所有所有,转载,归因:http://blog.csdn.net/wanghantong

TestNg使用dependsOnGroups属性来进行依赖測试,

測试方法依赖于某个或某些方法,这个/这些方法作为前置依赖条件

强制依赖:假设被依赖的某一个方法发生了异常,那么之后的方法都不会被运行(默认)

顺序依赖:不管被依赖的方法是否出现异常,兴许的方法都会被运行,通过alwaysRun=“true”来配置



/**
 * 
 * <p>
 * Title: TestngDependOnGroups
 * </p>
 * 
 * <p>
 * Description: 測试方法依赖于某个或某些方法,这个/这些方法作为前置依赖条件
 * 
 * 假设依赖的方法发生异常而不能正确的运行时,默认情况下,接下来的方法将不会被运行(强制依赖,默认) 假设在方法体的注解上设置了alwaysRun =
 * true,那么此时在依赖链中的不论什么一个方法出现异常都不会影响其他方法的运行
 * 
 * dependsOnGroups
 * 
 * </p>
 * 
 * <p>
 * Company:
 * </p>
 * 
 * @author : Dragon
 * 
 * @date : 2014年10月21日
 */
public class TestngDependOnGroups {
	@Test(groups = { "init" })
	public void serverStartedOk() {
		System.out.println("serverStartedOk.....");
	}

	@Test(groups = { "init2" })
	public void initEnvironment() {

		System.out.println("initEnvironment.....");
		throw new RuntimeException("unexpected fail......");
	}

	@Test(dependsOnGroups = { "init.*" }, alwaysRun = true)
	public void method1() {
		System.err.println("I am over here.....");
	}
}

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="framework_testng" >
	<test verbose="2" name="TestngDependOnGroups">
		<classes>
			<class name="com.dragon.testng.annotation.TestngDependOnGroups">
			</class>
		</classes>
	</test>
</suite>

执行结果:我们发如今配置了顺序依赖之后,即使initEnvironment()方法抛出了异常,method1()还是执行了而且passed

initEnvironment.....
serverStartedOk.....
I am over here.....
PASSED: serverStartedOk
PASSED: method1
FAILED: initEnvironment
java.lang.RuntimeException: unexpected fail......
	at com.dragon.testng.annotation.TestngDependOnGroups.initEnvironment(TestngDependOnGroups.java:41)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	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.RemoteTestNG.run(RemoteTestNG.java:111)
	at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
	at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)


===============================================
    TestngDependOnGroups
    Tests run: 3, Failures: 1, Skips: 0
===============================================


默认的强制依赖:Java code:

public class TestngDependOnGroups {
	@Test(groups = { "init" })
	public void serverStartedOk() {
		System.out.println("serverStartedOk.....");
	}

	@Test(groups = { "init2" })
	public void initEnvironment() {

		System.out.println("initEnvironment.....");
		throw new RuntimeException("unexpected fail......");
	}

	@Test(dependsOnGroups = { "init.*" })
	public void method1() {
		System.err.println("I am over here.....");
	}
}

原创文章,版权全部。同意转载。标明出处:http://blog.csdn.net/wanghantong

执行结果: 这时我们发现initEnvironment() 抛出异常后, method1() 被跳过了,并没有被执行

initEnvironment.....
serverStartedOk.....
PASSED: serverStartedOk
FAILED: initEnvironment
java.lang.RuntimeException: unexpected fail......
	at com.dragon.testng.annotation.TestngDependOnGroups.initEnvironment(TestngDependOnGroups.java:41)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	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.RemoteTestNG.run(RemoteTestNG.java:111)
	at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
	at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

SKIPPED: method1

===============================================
    TestngDependOnGroups
    Tests run: 3, Failures: 1, Skips: 1
===============================================


TestNg依赖具体解释(三)------灵活的文件配置依赖


版权声明:本文博客原创文章。博客,未经同意,不得转载。

你可能感兴趣的:(TestNG)