2014-09-30 66 views

回答

28

你不能从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); 
     } 
    } 

} 

然后:

+0

这太糟糕了,因为能够在XML中配置而不是代码对于某些用例非常重要。例如:我有一个功能测试用例purchaseXYZ()。在我的功能测试套件中,我只是想一次运行它来查看是否有任何问题。在我的性能测试套件中,我想运行它100次以获得平均延迟。因此我需要能够指定来自XML的调用次数,而不是在代码中进行硬编码。 – 2015-08-27 19:03:19

+1

为什么不直接进行第二次测试 - 一个用于功能测试,另一个用于单元测试? – anon58192932 2017-05-17 00:29:29

+0

@ anon58192932尽管我可以看到这会起作用,但它似乎更像是一种解决方法而不是解决方案。 – 2017-06-23 14:04:37

0

如果你不介意使用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是的次数你想测试重复。

+0

我正在使用testng和数据提供程序。我该怎么办?现在我操纵ojects数组的大小。你认为这是一个好主意吗? – 2014-10-15 16:40:45

+0

我想你的意思是“如果你不介意使用** Spring ** ....”还要注意,这是一个关于TestNG而不是JUnit的问题。 – 2017-06-23 13:56:40

1

你不能从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/"} 
    }; 
} 
3

TestNG的有一个方法。您可以使用此方法和运行测试用例多次:

@Test(invocationCount = 100) 

public void testCount() { 

} 
+0

你可以请[编辑]你的答案,并澄清你的意思?现在,我不明白你是否给出了新的答案或评论[niharika_neo's answer](http://stackoverflow.com/a/26134824/3982001)。如果你想问一些问题,你应该在一个新的问题上做,而不是在这里。这是一个问答网站,而不是论坛。谢谢! – 2017-01-31 14:00:24

+0

感谢您的确认。 – 2017-01-31 15:29:24

0

我知道非常迟到了,但如果你的目标是每次运行实现报告,那么你可以尝试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> 

+0

这看起来不错。但是,你能从testng.xml文件中获得numberOftimesTOExecute变量吗? – 2017-06-23 13:54:31

+0

可以创建一个“服务加载器”。看到这个问题的答案:https://stackoverflow.com/questions/45593426/testng-set-invocationcount-globally – Andrejs 2017-08-23 20:42:12

2

到目前为止,没有任何答案能够让用户从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> 
相关问题