Java自動化測試框架-10 – TestNG之測試結果篇
- 2019 年 11 月 11 日
- 筆記
1.-測試結果
1.1-成功,失敗和斷言
測試被認為是成功的,如果它不引發任何異常完成,還是它扔的預期異常(請參閱文檔expectedExceptions屬性上找到的@Test注釋)。
您的測試方法通常由可能引發異常的調用或各種斷言(使用Java“ assert”關鍵字)組成。“斷言”失敗將觸發AssertionErrorException,這反過來會將方法標記為失敗(如果未看到斷言錯誤,請記住在JVM上使用-ea)。
這是一個示例測試方法:
/** * @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類,該類使您可以對複雜對象執行斷言:
/** * @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和文本文件。
使用TestNG與監聽器和報告器生成自己的報告非常容易:
偵聽器實現org.testng.ITestListener介面,並在測試開始,通過,失敗等時實時通知。
報告程式實現org.testng.IReporter介面,並在TestNG已運行所有套件時收到通知。IReporter實例接收描述整個測試運行的對象列表。
例如,如果要生成測試運行的PDF報告,則無需實時通知測試運行,因此您應該使用IReporter。如果您想編寫測試的實時報告,例如帶有進度條的GUI或在每次測試被調用時顯示點(“。”)的文本報告程式(如下所述),則ITestListener是您的最好的選擇。
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 testtestng.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 | A 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之測試結果,就分享到這裡。