Java自動化測試框架-10 - TestNG之測試結果篇

1.-測試結果

1.1-成功,失敗和斷言

測試被認爲是成功的,若是它不引起任何異常完成,仍是它扔的預期異常(請參閱文檔expectedExceptions屬性上找到的@Test註釋)。html

您的測試方法一般由可能引起異常的調用或各類斷言(使用Java「 assert」關鍵字)組成。「斷言」失敗將觸發AssertionErrorException,這反過來會將方法標記爲失敗(若是未看到斷言錯誤,請記住在JVM上使用-ea)。java

這是一個示例測試方法:框架

/**
 * @author 北京-宏哥
 * 
 * Java自動化測試框架-10 - TestNG之 測試結果篇
 *
 * 2019年11月9日
 */
@Test
public void verifyLastName() {
  assert "Beust".equals(m_lastName) : "Expected name Beust, for" + m_lastName;
}

TestNG還包括JUnit的Assert類,該類使您能夠對複雜對象執行斷言:ide

/**
 * @author 北京-宏哥
 * 
 * Java自動化測試框架-10 - TestNG之 測試結果篇
 *
 * 2019年11月9日
 */
import static org.testng.AssertJUnit.*;
//...
@Test
public void verify() {
  assertEquals("Beust", m_lastName);
}

請注意,上面的代碼使用靜態導入,以便可以使用 assertEquals方法而沒必要在其類以前添加前綴。測試

1.2-記錄和結果

測試運行的結果在啓動SuiteRunner時指定的目錄中的index.html文件中建立。該文件指向包含整個測試運行結果的各類其餘HTML和文本文件。ui

使用TestNG與監聽器和報告器生成本身的報告很是容易:this

偵聽器實現org.testng.ITestListener接口,並在測試開始,經過,失敗等時實時通知。spa

報告程序實現org.testng.IReporter接口,並在TestNG已運行全部套件時收到通知。IReporter實例接收描述整個測試運行的對象列表。命令行

例如,若是要生成測試運行的PDF報告,則無需實時通知測試運行,所以您應該使用IReporter。若是您想編寫測試的實時報告,例如帶有進度條的GUI或在每次測試被調用時顯示點(「。」)的文本報告程序(以下所述),則ITestListener是您的最好的選擇。code

1.2.1-記錄偵聽器

這是一個顯示「。」的偵聽器。對於每一個經過的測試,對於每一個失敗,都爲「 F」,對於每一個跳過均爲「 S」:

/**
 * @author 北京-宏哥
 * 
 * Java自動化測試框架-10 - TestNG之 測試結果篇
 *
 * 2019年11月9日
 */
public class DotTestListener extends TestListenerAdapter {
  private int m_count = 0;
 
  @Override
  public void onTestFailure(ITestResult tr) {
    log("F");
  }
 
  @Override
  public void onTestSkipped(ITestResult tr) {
    log("S");
  }
 
  @Override
  public void onTestSuccess(ITestResult tr) {
    log(".");
  }
 
  private void log(String string) {
    System.out.print(string);
    if (++m_count % 40 == 0) {
      System.out.println("");
    }
  }
}

在此示例中,我選擇擴展TestListenerAdapter,該方法使用空方法實現ITestListener,所以我沒必要從我不感興趣的接口中覆蓋其餘方法。您能夠根據須要直接實現該接口。

這是我調用TestNG來使用此新偵聽器的方法:

java -classpath testng.jar;%CLASSPATH% org.testng.TestNG -listener org.testng.reporters.DotTestListener test\testng.xml

和輸出:

........................................
........................................
........................................
........................................
........................................
.........................
===============================================
TestNG JDK 1.5
Total tests run: 226, Failures: 0, Skips: 0
===============================================

請注意,當您使用-listener時,TestNG將自動肯定您要使用的偵聽器的類型。

1.2.2-記錄記者

該org.testng.IReporter接口只有一個方法:

public void generateReport(List<ISuite> suites, String outputDirectory)

當全部套件都已運行時,TestNG將調用此方法,您能夠檢查其參數以訪問剛剛完成的運行中的全部信息。

1.2.3-JUnitReports

TestNG包含一個偵聽器,該偵聽器獲取TestNG結果並輸出一個XML文件,而後能夠將其饋送到JUnitReport。 這是一個示例,以及建立此報告的ant任務:

<target name="reports">
  <junitreport todir="test-report">
    <fileset dir="test-output">
      <include name="*/*.xml"/>
    </fileset>
  
    <report format="noframes"  todir="test-report"/>
  </junitreport>
</target>

注意:JDK 1.5和JUnitReports當前不兼容,沒法使用框架版本,所以您須要指定「 noframes」才能使其正常工做。

1.2.4-Reporter API

若是須要記錄應在生成的HTML報告中顯示的消息,則能夠使用org.testng.Reporter類:

Reporter.log (「已呼叫M3」 );

     

1.2.5-XML報告

TestNG提供了一個XML報告程序,用於捕獲JUnit報告中不提供的TestNG特定信息。當用戶的測試環境須要使用JUnit格式沒法提供的具備TestNG特定數據的XML結果時,此功能特別有用。記者能夠經過使用命令行注入TestNG的-reporter。

這是一個示例用法:-reporter org.testng.reporters.XMLReporter:generateTestResultAttributes = true,generateGroupsAttribute = true。

下表詳細介紹了能夠傳遞的全部選項。確保使用:

: -將報告者名稱與其屬性分開

= -分隔屬性的鍵/值對

, -分隔多個鍵/值對

如下是此類報告器的輸出示例:

<testng-results>
  <suite name="Suite1">
    <groups>
      <group name="group1">
        <method signature="com.test.TestOne.test2()" name="test2" class="com.test.TestOne"/>
        <method signature="com.test.TestOne.test1()" name="test1" class="com.test.TestOne"/>
      </group>
      <group name="group2">
        <method signature="com.test.TestOne.test2()" name="test2" class="com.test.TestOne"/>
      </group>
    </groups>
    <test name="test1">
      <class name="com.test.TestOne">
        <test-method status="FAIL" signature="test1()" name="test1" duration-ms="0"
              started-at="2007-05-28T12:14:37Z" description="someDescription2"
              finished-at="2007-05-28T12:14:37Z">
          <exception class="java.lang.AssertionError">
            <short-stacktrace>
              <![CDATA[
                java.lang.AssertionError
                ... Removed 22 stack frames
              ]]>
            </short-stacktrace>
          </exception>
        </test-method>
        <test-method status="PASS" signature="test2()" name="test2" duration-ms="0"
              started-at="2007-05-28T12:14:37Z" description="someDescription1"
              finished-at="2007-05-28T12:14:37Z">
        </test-method>
        <test-method status="PASS" signature="setUp()" name="setUp" is-config="true" duration-ms="15"
              started-at="2007-05-28T12:14:37Z" finished-at="2007-05-28T12:14:37Z">
        </test-method>
      </class>
    </test>
  </suite>
</testng-results>

該報告程序與其餘默認偵聽器一塊兒注入,所以默認狀況下您能夠得到這種類型的輸出。偵聽器提供了一些屬性,能夠對報告器進行調整以知足您的需求。下表包含這些屬性的列表,並附有簡短說明:

Property Comment Default value
outputDirectory String indicating the directory where should the XML files be output. The TestNG output directory
timestampFormat Specifies the format of date fields that are generated by this reporter yyyy-MM-dd'T'HH:mm:ss'Z'
fileFragmentationLevel An integer having the values 1, 2 or 3, indicating the way that the XML files are generated:
   1 - will generate all the results in one file.
   2 - each suite is generated in a separate XML file that is linked to the main file.
   3 - same as 2 plus separate files for test-cases that are referenced from the suite files.
1
splitClassAndPackageNames This boolean specifies the way that class names are generated for the <class> element. For example, you will get <class class="com.test.MyTest"> for false and <class class="MyTest" package="com.test"> for true. false
generateGroupsAttribute A boolean indicating if a groups attribute should be generated for the <test-method> element. This feature aims at providing a straight-forward method of retrieving the groups that include a test method without having to surf through the <group> elements. false
generateTestResultAttributes A boolean indicating if an <attributes> tag should be generated for each <test-method> element, containing the test result attributes (See ITestResult.setAttribute() about setting test result attributes). Each attribute toString() representation will be written in a <attribute name="[attribute name]"> tag. false
stackTraceOutputMethod Specifies the type of stack trace that is to be generated for exceptions and has the following values:
   0 - no stacktrace (just Exception class and message).
   1 - a short version of the stack trace keeping just a few lines from the top
   2 - the complete stacktrace with all the inner exceptions
   3 - both short and long stacktrace
2
generateDependsOnMethods Use this attribute to enable/disable the generation of a depends-on-methods attribute for the <test-method> element. true
generateDependsOnGroups Enable/disable the generation of a depends-on-groups attribute for the <test-method> element. true

爲了配置此報告程序,能夠在命令行中使用-reporter選項,也能夠將Ant 任務與嵌套的<reporter>元素一塊兒使用。對於其中的每一個,您都必須指定org.testng.reporters.XMLReporter類。請注意,您沒法配置內置報告器,由於該報告器僅使用默認設置。若是隻須要

帶有自定義設置的XML報告,則必須使用兩種方法之一手動添加它並禁用默認偵聽器。

1.2.6-TestNG退出代碼

當TestNG完成執行時,它將退出並返回代碼。

能夠檢查此返回碼以瞭解故障的性質(若是有的話)。

下表總結了TestNG當前使用的不一樣退出代碼。

FailedWithinSuccess Skipped Failed Status Code Remarks
No No No 0 Passed tests
No No Yes 1 Failed tests
No Yes No 2 Skipped tests
No Yes Yes 3 Skipped/Failed tests
Yes No No 4 FailedWithinSuccess tests
Yes No Yes 5 FailedWithinSuccess/Failed tests
Yes Yes No 6 FailedWithinSuccess/Skipped tests
Yes Yes Yes 7 FailedWithinSuccess/Skipped/Failed tests

2.-小結

  好了,今天關於TestNG之測試結果,就分享到這裏。

相關文章
相關標籤/搜索