有时我们会遇到这样的问题,比如如何多次运行一个测试用例?invocationCount是这个问题的答案。在这篇文章中,我们将讨论在TestNG中与@Test annotation一起使用的invocationCount属性。
这个属性有什么作用,或者调用计数有什么用?invocationCount有助于多次执行单个测试用例。因此,如果您有一个需要多次运行的测试用例,invocationCount可以帮助您。
语法
@Test(invocationCount = num)其中num =您希望运行此测试方法的次数。
示例:
package Test;import org.testng.Assert;
import org.testng.annotations.Test;public class CodekruTest {@Test(invocationCount = 5) // now, this test case will run 5 timespublic void test2() {System.out.println("test2 is passed");Assert.assertTrue(true);}}
下面是运行上述测试类的XML文件
<suite name="codekru"><test name="codekruTest"><classes><class name="Test.CodekruTest"></class></classes></test>
</suite>
运行上述XML文件后的输出-
test2 is passed
test2 is passed
test2 is passed
test2 is passed
test2 is passed
PASSED: test2
PASSED: test2
PASSED: test2
PASSED: test2
PASSED: test2===============================================Default testTests run: 5, Failures: 0, Skips: 0
===============================================
所以,在这里我们可以看到test()运行了五次。
假设情景
如果我们保持invocationCount =0呢?到时候会发生什么?
在这里,测试用例甚至不会运行,如下面的示例所示
package Test;import org.testng.Assert;
import org.testng.annotations.Test;public class CodekruTest {@Test(invocationCount = 0) // now, this test case will run 5 timespublic void test2() {System.out.println("test2 is passed");Assert.assertTrue(true);}}
运行相同的XML文件后的输出-
===============================================
codekru
Total tests run: 0, Passes: 0, Failures: 0, Skips: 0
===============================================
在这里我们可以看到没有执行任何测试用例或方法。
如果我们将invocationCount保持为负值呢?
它不会给予任何错误,但是测试用例不会被执行。
package Test;import org.testng.Assert;
import org.testng.annotations.Test;public class CodekruTest {@Test(invocationCount = -2) // now, this test case will run 5 timespublic void test2() {System.out.println("test2 is passed");Assert.assertTrue(true);}}
产出-
===============================================
codekru
Total tests run: 0, Passes: 0, Failures: 0, Skips: 0
===============================================