本文共 2094 字,大约阅读时间需要 6 分钟。
基于某种测试场景,或者处于某种需求考虑,部分测试方法需要暂时不要参加测试活动,此时,我们可以使用TestNG中@Test(enabled = false)禁用此测试方法。
默认情况下,enabled属性值为true,也就是说缺省该属性时,Testng会始终运行该测试方法。
现在,让我们来看看测试@Test(enabled = false) 动作。
创建一个Java类进行测试为 MessageUtil.java 在 C: > TestNG_WORKSPAC
/** This class prints the given message on console.*/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; } }
创建测试类为 IgnoreTest.java.
测试类添加测试方法testPrintMessage(),testSalutationMessage()。
添加注释 @Test(enabled = false) 到方法 testPrintMessage().
创建一个Java类文件名 IgnoreTest.java 在 C: > TestNG_WORKSPACE
import org.testng.Assert;
import org.testng.annotations.Test;public class IgnoreTest { String message = "Manisha"; MessageUtil messageUtil = new MessageUtil(message); @Test(enabled = false) public void testPrintMessage() { System.out.println("Inside testPrintMessage()"); message = "Manisha"; Assert.assertEquals(message, messageUtil.printMessage()); } @Test public void testSalutationMessage() { System.out.println("Inside testSalutationMessage()"); message = "Hi!" + "Manisha"; Assert.assertEquals(message, messageUtil.salutationMessage()); }}
创建一个文件 testng.xml C: > TestNG_WORKSPACE 用来执行测试案例
编译MessageUtil的测试用例类使用javac。
C:TestNG_WORKSPACE>javac MessageUtil.java IgnoreTest.java
现在,运行testng.xml,将无法运行testPrintMessage()定义的测试用例在测试案例类。
C:TestNG_WORKSPACE>java -cp "C:TestNG_WORKSPACE" org.testng.TestNG testng.xml
验证输出。 testPrintMessage()测试用例没有测试。
Inside testSalutationMessage()Hi!Manisha===============================================Suite1Total tests run: 1, Failures: 0, Skips: 0===============================================
转载地址:http://fcrub.baihongyu.com/