您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > TestNG
TestNG自定義記錄器測試報告
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2014/9/4 13:33:17 ] 推薦標簽:軟件測試 單元測試 TestNG

  在本節(jié)中,我們將介紹一個例子,編寫自定義記錄器和TestNG的方法。要編寫一個定制的記錄器類,我們的擴展類應(yīng)實現(xiàn)IReporter接口。讓我們繼續(xù)前進,并創(chuàng)建一個示例使用自定義的記錄器。
  創(chuàng)建測試案例類
  創(chuàng)建一個Java類為 SampleTest.java 在 C: > TestNG_WORKSPACE
import org.testng.Assert;
import org.testng.annotations.Test;
public class SampleTest {
@Test
public void testMethodOne(){
Assert.assertTrue(true);
}
@Test
public void testMethodTwo(){
Assert.assertTrue(false);
}
@Test(dependsOnMethods={"testMethodTwo"})
public void testMethodThree(){
Assert.assertTrue(true);
}
}
  上述測試類的包含三個測試方法,其中testMethodOne 和 testMethodThree將通過在執(zhí)行時,而testMethodTwo由通過一個falseBoolean的值A(chǔ)ssert.assertTrue方法,它是用于在測試中的真值條件失敗。
  創(chuàng)建自定義報告類
  創(chuàng)建另一個新的類名為 CustomReporter.java 在 C: > TestNG_WORKSPACE
import java.util.List;
import java.util.Map;
import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestContext;
import org.testng.xml.XmlSuite;
public class CustomReporter implements IReporter{
@Override
public void generateReport(List xmlSuites, List suites,
String outputDirectory) {
//Iterating over each suite included in the test
for (ISuite suite : suites) {
//Following code gets the suite name
String suiteName = suite.getName();
//Getting the results for the said suite
Map<string, isuiteresult=""> suiteResults = suite.getResults();
for (ISuiteResult sr : suiteResults.values()) {
ITestContext tc = sr.getTestContext();
System.out.println("Passed tests for suite '" + suiteName +
"' is:" + tc.getPassedTests().getAllResults().size());
System.out.println("Failed tests for suite '" + suiteName +
"' is:" +
tc.getFailedTests().getAllResults().size());
System.out.println("Skipped tests for suite '" + suiteName +
"' is:" +
tc.getSkippedTests().getAllResults().size());
}
}
}
}
  前面的的類實現(xiàn)org.testng.IReporter 接口。它實現(xiàn)了IReporter接口定義的方法GenerateReport。這個方法有三個參數(shù):
  第一個是xmlSuite,這是TestNG的測試XML正在執(zhí)行中提到的列表套件
  第二個是套件,其中包含一套測試執(zhí)行后信息,該對象包含了所有的信息包,類,測試方法和測試執(zhí)行結(jié)果。
  第三的outputDirectory,報告將產(chǎn)生的輸出文件夾路徑,其中包含的信息。

上一頁12下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd