2014-09-30 62 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> 
相關問題