qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請?jiān)L問 http://qaseven.github.io/

          TestNG自定義記錄器測試報(bào)告

          在本節(jié)中,我們將介紹一個(gè)例子,編寫自定義記錄器和TestNG的方法。要編寫一個(gè)定制的記錄器類,我們的擴(kuò)展類應(yīng)實(shí)現(xiàn)IReporter接口。讓我們繼續(xù)前進(jìn),并創(chuàng)建一個(gè)示例使用自定義的記錄器。
            創(chuàng)建測試案例類
            創(chuàng)建一個(gè)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);
          }
          }
            上述測試類的包含三個(gè)測試方法,其中testMethodOne 和 testMethodThree將通過在執(zhí)行時(shí),而testMethodTwo由通過一個(gè)falseBoolean的值A(chǔ)ssert.assertTrue方法,它是用于在測試中的真值條件失敗。
            創(chuàng)建自定義報(bào)告類
            創(chuàng)建另一個(gè)新的類名為 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());
          }
          }
          }
          }
            前面的的類實(shí)現(xiàn)org.testng.IReporter 接口。它實(shí)現(xiàn)了IReporter接口定義的方法GenerateReport。這個(gè)方法有三個(gè)參數(shù):
            第一個(gè)是xmlSuite,這是TestNG的測試XML正在執(zhí)行中提到的列表套件
            第二個(gè)是套件,其中包含一套測試執(zhí)行后信息,該對象包含了所有的信息包,類,測試方法和測試執(zhí)行結(jié)果。
            第三的outputDirectory,報(bào)告將產(chǎn)生的輸出文件夾路徑,其中包含的信息。
          創(chuàng)建 testng.xml
            創(chuàng)建一個(gè)文件testng.xml 在 C:\ > TestNG_WORKSPACE 來執(zhí)行測試用例
          <?xml version="1.0" encoding="UTF-8"?>
          <suite name="Simple Reporter Suite">
          <listeners>
          <listener class-name="CustomReporter" />
          </listeners>
          <test name="Simple Reporter test">
          <classes>
          <class name="SampleTest" />
          </classes>
          </test>
          </suite>
            編譯SampleTest,CustomReporter類使用javac
            C:\TestNG_WORKSPACE>javac CustomReporter.java SampleTest.java
            運(yùn)行 testng.xml.
            C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml
            驗(yàn)證輸出
            ===============================================
            Simple Reporter Suite
            Total tests run: 3, Failures: 1, Skips: 1
            ===============================================
            Passed tests for suite 'Simple Reporter Suite' is:1
            Failed tests for suite 'Simple Reporter Suite' is:1
            Skipped tests for suite 'Simple Reporter Suite' is:1
            前面的例子顯示了一個(gè)簡單的自定義報(bào)告器,打印的數(shù)量在控制臺(tái)上對每個(gè)套件包含在上述的測試執(zhí)行失敗,通過跳過測試。報(bào)告器主要是用于測試的執(zhí)行,以生成最終的報(bào)告。擴(kuò)展程序可以被用來生成XML,HTML,CHM,CSV或文本格式的文件,根據(jù)報(bào)告要求。

          posted on 2014-09-05 09:41 順其自然EVO 閱讀(727) 評(píng)論(0)  編輯  收藏 所屬分類: 測試學(xué)習(xí)專欄

          <2014年9月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 资兴市| 岳池县| 乐清市| 札达县| 即墨市| 冀州市| 宜良县| 西青区| 荣昌县| 瓮安县| 娱乐| 鄂尔多斯市| 绥德县| 彰武县| 福安市| 东乌珠穆沁旗| 巴中市| 东兴市| 绥江县| 平江县| 通州市| 孟连| 买车| 溆浦县| 阿尔山市| 耿马| 翁源县| 岳普湖县| 昭平县| 鄂尔多斯市| 会东县| 新安县| 安康市| 金平| 淮南市| 上饶县| 博乐市| 甘孜| 庆安县| 三穗县| 金阳县|