我想多次运行测试用例。可以在testng.xml
中配置吗?如果我在测试方法中添加循环,则每个运行的结果在testng
报告中都不会受到影响。Testng Webdriver:我怎样才能多次运行相同的测试用例?
回答
你不能从xml中完成它,而是在@Test注解中 - 你可以添加一个invocationCount属性和你想运行它的次数。它会在报告中运行的许多测试中出现。
例如。
@Test(invocationCount = 10)
public void testCount() {..}
你错过了最后的大括号,所以小修正。在实际测试中
package somePackage;
import org.junit.Ignore;
import org.junit.runner.Description;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
import org.springframework.test.annotation.Repeat;
public class ExtendedRunner extends BlockJUnit4ClassRunner {
public ExtendedRunner(Class<?> klass) throws InitializationError {
super(klass);
}
@Override
protected Description describeChild(FrameworkMethod method) {
if (method.getAnnotation(Repeat.class) != null
&& method.getAnnotation(Ignore.class) == null) {
return describeRepeatTest(method);
}
return super.describeChild(method);
}
private Description describeRepeatTest(FrameworkMethod method) {
int times = method.getAnnotation(Repeat.class).value();
Description description = Description.createSuiteDescription(
testName(method) + " [" + times + " times]",
method.getAnnotations());
for (int i = 1; i <= times; i++) {
description.addChild(Description.createTestDescription(
getTestClass().getJavaClass(), "[" + i + "] "
+ testName(method)));
}
return description;
}
@Override
protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
Description description = describeChild(method);
if (method.getAnnotation(Repeat.class) != null
&& method.getAnnotation(Ignore.class) == null) {
runRepeatedly(methodBlock(method), description, notifier);
}
super.runChild(method, notifier);
}
private void runRepeatedly(Statement statement, Description description,
RunNotifier notifier) {
for (Description desc : description.getChildren()) {
runLeaf(statement, desc, notifier);
}
}
}
然后:
这太糟糕了,因为能够在XML中配置而不是代码对于某些用例非常重要。例如:我有一个功能测试用例purchaseXYZ()。在我的功能测试套件中,我只是想一次运行它来查看是否有任何问题。在我的性能测试套件中,我想运行它100次以获得平均延迟。因此我需要能够指定来自XML的调用次数,而不是在代码中进行硬编码。 – 2015-08-27 19:03:19
为什么不直接进行第二次测试 - 一个用于功能测试,另一个用于单元测试? – anon58192932 2017-05-17 00:29:29
@ anon58192932尽管我可以看到这会起作用,但它似乎更像是一种解决方法而不是解决方案。 – 2017-06-23 14:04:37
如果你不介意使用Sprint的,你可以创建这个类
package somePackage;
import *.ExtendedRunner;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.springframework.test.annotation.Repeat;
@Ignore
@RunWith(ExtendedRunner.class)
public class RepeatedTest{
@Repeat(value N)
public void testToBeRepeated() {
}
}
其中N是的次数你想测试重复。
我正在使用testng和数据提供程序。我该怎么办?现在我操纵ojects数组的大小。你认为这是一个好主意吗? – 2014-10-15 16:40:45
我想你的意思是“如果你不介意使用** Spring ** ....”还要注意,这是一个关于TestNG而不是JUnit的问题。 – 2017-06-23 13:56:40
你不能从xml中完成它,但它可以通过在TestNG中使用@DataProvider注释来实现。
下面是一个示例代码:
/* Since Data provider for this test method returns 2D array of size 3x1,
this test method will run 3 times **automatically** with 1 parameter every time. */
@Test(dataProvider="URLprovider")
private void notePrice(String url) {
driver.get(url);
System.out.println(driver.getTitle());
}
// It will return a 2D array of size 3x1
@DataProvider(name="URLprovider")
private Object[][] getURLs() {
return new Object[][] {
{"https://www.google.co.in/"},
{"http://www.gmail.com/"},
{"http://stackoverflow.com/"}
};
}
TestNG的有一个方法。您可以使用此方法和运行测试用例多次:
@Test(invocationCount = 100)
public void testCount() {
}
你可以请[编辑]你的答案,并澄清你的意思?现在,我不明白你是否给出了新的答案或评论[niharika_neo's answer](http://stackoverflow.com/a/26134824/3982001)。如果你想问一些问题,你应该在一个新的问题上做,而不是在这里。这是一个问答网站,而不是论坛。谢谢! – 2017-01-31 14:00:24
感谢您的确认。 – 2017-01-31 15:29:24
我知道非常迟到了,但如果你的目标是每次运行实现报告,那么你可以尝试TestNG的监听IAnnotationTransformer
代码片断
public class Count implements IAnnotationTransformer {
@Override
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
// TODO Auto-generated method stub
annotation.setInvocationCount(numberOfTimesTOExecute);
}
XML片段
<listeners>
<listener class-name="multiple.Count"></listener>
这看起来不错。但是,你能从testng.xml文件中获得numberOftimesTOExecute变量吗? – 2017-06-23 13:54:31
可以创建一个“服务加载器”。看到这个问题的答案:https://stackoverflow.com/questions/45593426/testng-set-invocationcount-globally – Andrejs 2017-08-23 20:42:12
到目前为止,没有任何答案能够让用户从testng文件中调用计数,这是要求的。该解决方案搭载了gaurav25的DataProvider解决方案。
class TestClass() {
int invocationCount;
@Parameters({ "invocationCount" })
@BeginClass
void BeginClass(@Optional("1") String invocationCount) {
this.invocationCount = Ingeter.parse(invocationCount)
}
// It will return a 2D array of size 3x1
@DataProvider(name="URLprovider")
private Object[][] getURLs() {
ArrayList<Object []> obj = new ArrayList<>(3 * this.invocationCount);
for(int iCount = 0; iCount < this.invocationCount; ++iCount) {
list.add(new Object[] {"https://www.google.co.in/"});
list.add(new Object[] {"http://www.gmail.com/"});
list.add(new Object[] {"http://stackoverflow.com/"});
}
return list.toArray();
}
/* Since Data provider for this test method returns 2D array of size
(3*invocationCount)x1, this test method will run 3*invocationCount
times **automatically** with 1 parameter every time. */
@Test(dataProvider="URLprovider")
private void notePrice(String url) {
driver.get(url);
System.out.println(driver.getTitle());
}
}
现在你可以改变多少测试集打通这个文件的testng.xml测试功能运行:
<suite name="ESFService" verbose="1" parallel="methods" thread-count="1" data-provider-thread-count="10" >
<test name="Basic">
<classes>
<class name="TestClass">
<parameter name="invocationCount" value="5"/>
</class>
</classes>
</test>
</suite>
- 1. 我怎样才能同时开多个(多个)黄瓜测试?
- 2. 使用jQuery,我怎样才能多次运行这个动画?
- 3. 我怎样才能多次调用setInterval?
- 4. 我怎样才能一次运行NHibernate的多个插入?
- 5. 我怎样才能运行一个Coded的UI测试同时运行在本地主机,我想测试
- 6. 我怎样才能在相同的ListView
- 7. Rails - 我怎样才能运行黄瓜测试?
- 8. testng多次运行硒测试
- 9. 我怎样才能同步执行DOH测试?
- 10. 运行TestNG测试实例
- 11. 我怎样才能同时运行多个动画?
- 12. 如何像参数化一样多次运行TestNG测试?
- 13. 运行测试用例TestNG的框架
- 14. 在testNG的多个URL上运行相同的测试
- 15. 使用不同的数据多次运行相同的JUnit测试用例
- 16. 我怎样才能切换多个图层相同的精灵?
- 17. 使用webdriver-factory运行Selenium Webdriver TestNG测试
- 18. 用不同的设置多次运行相同的测试
- 19. 运行WEbDriver测试
- 20. 我怎样才能使用我的自定义下拉多次
- 21. 使用watir-webdriver,我怎样才能点击一个表格行?
- 22. 从单个测试用例中运行多个TestNG测试用例
- 23. WHMCS,我怎样才能运行代码只有一次
- 24. 在同一个浏览器中运行所有测试webdriver testng
- 25. 只有当我使用Maven运行测试用例时,TestNG测试结果控制台才为空(测试)
- 26. 我怎样才能在每个脚本中运行相同的代码?
- 27. 执行多个测试依次用不同的参数TestNG的
- 28. 测试用例未运行iin selenium webdriver
- 29. webdriver的在Ruby中,如何运行测试用例x次
- 30. 我怎样才能调用函数一次点击JavaScript它只运行一次
如何运行相同的测试多时,但那个时候我之间运行其他测试方法 ? – Emna 2017-02-08 10:02:49