赞
踩
有时候,你可能需要在一个特定的顺序调用方法在测试案例,或者你想分享一些数据和方法之间的状态。testNg允许指定依赖
使用属性dependsOnMethods在@Test注释OR
使用属性dependsOnGroups在@Test注解
- package depends;
-
- public class MessageUtil {
- private String message;
-
- // Constructor
- // @param message to be printed
- public MessageUtil(String message) {
- this.message = message;
- }
-
- // prints the message
- public String printMessage() {
- System.out.println(message);
- return message;
- }
-
- // add "Hi!" to the message
- public String salutationMessage() {
- message = "Hi!" + message;
- System.out.println(message);
- return message;
- }
- }
创建一个Java测试类为DependencyTestUsingAnnotation.java.
添加方法testPrintMessage(),testSalutationMessage()和initEnvironmentTest()到测试类
添加属性dependsOnMethods= { "initEnvironmentTest" }to the @Test注释oftestSalutationMessage()方法
- package depends;
-
- import org.testng.Assert;
- import org.testng.annotations.Test;
-
- public class DependencyTestUsingAnnotation {
- String message = "Manisha";
- MessageUtil messageUtil = new MessageUtil(message);
-
- @Test
- public void testPrintMessage() {
- System.out.println("Inside testPrintMessage()");
- message = "Manisha";
- Assert.assertEquals(message, messageUtil.printMessage());
- }
-
- @Test(dependsOnMethods = { "initEnvironmentTest" })
- public void testSalutationMessage() {
- System.out.println("Inside testSalutationMessage()");
- message = "Hi!" + "Manisha";
- Assert.assertEquals(message, messageUtil.salutationMessage());
- }
-
- @Test
- public void initEnvironmentTest() {
- System.out.println("This is initEnvironmentTest");
- }
- }
</pre><pre>
- <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
- <suite name="Suite1">
- <test name="test1">
- <classes>
- <class name="DependencyTestUsingAnnotation" />
- </classes>
- </test>
- </suite>
也可以依赖于整个群组的方法
- package depends;
-
- public class MessageUtil {
- private String message;
- // Constructor
- // @param message to be printed
- public MessageUtil(String message) {
- this.message = message;
- }
-
- // prints the message
- public String printMessage() {
- System.out.println(message);
- return message;
- }
-
- // add "Hi!" to the message
- public String salutationMessage() {
- message = "Hi!" + message;
- System.out.println(message);
- return message;
- }
- }
创建一个Java测试类说依赖TestUsingAnnotation.java.
添加测试方法testPrintMessage(),testSalutationMessage()和initEnvironmentTest()测试类和他们的组"初始化"
添加属性dependsOnMethods= { "init.*" }to the @Test注释testSalutationMessage()方法
- package depends;
-
- import org.testng.Assert;
- import org.testng.annotations.Test;
-
- public class DependencyTestUsingAnnotation {
- String message = "Manisha";
- MessageUtil messageUtil = new MessageUtil(message);
-
- @Test(groups = { "init" })
- public void testPrintMessage() {
- System.out.println("Inside testPrintMessage()");
- message = "Manisha";
- Assert.assertEquals(message, messageUtil.printMessage());
- }
-
- @Test(dependsOnGroups = { "init.*" })
- public void testSalutationMessage() {
- System.out.println("Inside testSalutationMessage()");
- message = "Hi!" + "Manisha";
- Assert.assertEquals(message, messageUtil.salutationMessage());
- }
-
- @Test(groups = { "init" })
- public void initEnvironmentTest() {
- System.out.println("This is initEnvironmentTest");
- }
- }
在这个例子中,testSalutationMessage()被声明为根据任何一组匹配正则表达式“的init*”,这保证了,一种方法,testPrintMessage的()和initEnvironmentTest()将始终前testSalutationMessage()被调用。
如果一个方法失败,取决于你有一个很难依赖于它(alwaysRun=false,这是默认的),没有标记的方法依赖于它的失败,但作为跳过。跳过的方法将被报告为例如在最终报告(在HTML中,既不是红也不是绿的颜色),这是很重要的,因为跳过的方法不一定是失败。
- <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
- <suite name="Suite1">
- <test name="test1">
- <classes>
- <class name="depends.DependencyTestUsingAnnotation" />
- </classes>
- </test>
- </suite>
在使用组,我们不再面临重构的问题。只要我们不修改dependsOnGroups或组属性,我们的测试将继续运行,设立适当的依赖。
每当一个新的方法需要添加依存关系图中,我们需要做的就是把它正确的组中,并确保它依赖于正确的组。我们不需要修改任何其他方法。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。