2013-10-26 25 views
0

testNG數據提供程序功能讀取我的excel文件,並根據我在測試中提供多少行數據來確定執行測試的次數。testng數據提供程序顯示當前測試編號

我想知道有一些方法可以有TestNG的顯示當前的測試號,並且它的運行測試的總數。示例打印語句:「我目前正在運行測試3中10」,然後下一次迭代「我目前正在運行測試4中的10」。

乾杯

+0

你想使用TestNG的監聽 –

+0

代碼爲一個具體的例子將是有益的,尤其對於我們這些其Java仍然需要工作。 :)謝謝 – gorbysbm

回答

1

在你的類中有您的@Test S,你將需要把一個註解:

@Listeners({TestListener.class}) 
public class SomeTestClass { 

然後監聽器類看起來類似以下內容:

public class TestListener extends TestListenerAdapter { 
    @Override 
    public void onTestStart(ITestResult result){ 
     ...fancy code to output... 
    } 
} 

那個奇特的代碼可能是不同的東西。

result.getParameters()會給你你傳遞給@Test的參數。如果您可以使用這些參數來確定您迄今爲止運行過多少次,那就太好了。

你可以有一個靜態的整數,將存儲當前運行得來(或字符串和整數的靜態地圖,測試名稱映射到的時間運行次數。

不過,我認爲這顯示Running test 1 of 10是不是真的那麼有用,但我使用監聽器輸出的信息,我認爲result.getParameters()result其他功能都非常豐富,可以幫助你做出良好的輸出。

+0

謝謝。當我通過調試器運行這個時,「result」變量返回了大量的字段。試圖找出哪一個是「測試編號」仍然很難。我相信它在那裏!我最終在我用來驅動測試的Excel表格中使用了一個非常簡單的解決方法。 – gorbysbm

0

這裏是一個解決辦法,我使用Excel的數據。 在Excel工作表中創建一個名爲TestRowNumber的列作爲測試數據中的第一列。在第一個單元格中使用此公式= ROW()並將其複製到其下的所有其他單元格中。此公式會自動爲電子表格中的每一行編號。

那麼Java代碼可以是這樣的:

@Test(dataProvider = "DP_ourTest", invocationCount = 1, priority=2, enabled = true) 
    public void ourTest(String... excelData) throws Exception { 

//This will grab the value of the first data column (index 0) if your Excel Data. 
setTestNumber(Integer.parseInt(excelData[0])-1); 


//And in a separate method or class you would set this testnumber data for each run 

public static int testNumber; 
// getter for TestNumber: this tells the Data source which row to start from 
public static int getTestNumber() { 
    return testNumber; 
} 
// setter for TestNumber: this tells the Data source which row to start from 
public void setTestNumber(int testNumber) { 
    this.testNumber = testNumber; 
} 
相關問題