我想多次運行測試用例。可以在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