java自动化测试报告_Java自动化测试框架-10 - TestNG之测试结果篇(详细教程)
1.-測(cè)試結(jié)果
1.1-成功,失敗和斷言
測(cè)試被認(rèn)為是成功的,如果它不引發(fā)任何異常完成,還是它扔的預(yù)期異常(請(qǐng)參閱文檔expectedExceptions屬性上找到的@Test注釋)。
您的測(cè)試方法通常由可能引發(fā)異常的調(diào)用或各種斷言(使用Java“ assert”關(guān)鍵字)組成。“斷言”失敗將觸發(fā)AssertionErrorException,這反過來會(huì)將方法標(biāo)記為失敗(如果未看到斷言錯(cuò)誤,請(qǐng)記住在JVM上使用-ea)。
這是一個(gè)示例測(cè)試方法:
/***@author北京-宏哥
*
* Java自動(dòng)化測(cè)試框架-10 - TestNG之 測(cè)試結(jié)果篇
*
* 2019年11月9日*/@Testpublic voidverifyLastName() {assert "Beust".equals(m_lastName) : "Expected name Beust, for" +m_lastName;
}
TestNG還包括JUnit的Assert類,該類使您可以對(duì)復(fù)雜對(duì)象執(zhí)行斷言:
/***@author北京-宏哥
*
* Java自動(dòng)化測(cè)試框架-10 - TestNG之 測(cè)試結(jié)果篇
*
* 2019年11月9日*/
import static org.testng.AssertJUnit.*;//...
@Testpublic voidverify() {
assertEquals("Beust", m_lastName);
}
請(qǐng)注意,上面的代碼使用靜態(tài)導(dǎo)入,以便能夠使用 assertEquals方法而不必在其類之前添加前綴。
1.2-日志和結(jié)果
測(cè)試運(yùn)行的結(jié)果在啟動(dòng)SuiteRunner時(shí)指定的目錄中的index.html文件中創(chuàng)建。該文件指向包含整個(gè)測(cè)試運(yùn)行結(jié)果的各種其他HTML和文本文件。
使用TestNG與監(jiān)聽器和報(bào)告器生成自己的報(bào)告非常容易:
偵聽器實(shí)現(xiàn)org.testng.ITestListener接口,并在測(cè)試開始,通過,失敗等時(shí)實(shí)時(shí)通知。
報(bào)告程序?qū)崿F(xiàn)org.testng.IReporter接口,并在TestNG已運(yùn)行所有套件時(shí)收到通知。IReporter實(shí)例接收描述整個(gè)測(cè)試運(yùn)行的對(duì)象列表。
例如,如果要生成測(cè)試運(yùn)行的PDF報(bào)告,則無需實(shí)時(shí)通知測(cè)試運(yùn)行,因此您應(yīng)該使用IReporter。如果您想編寫測(cè)試的實(shí)時(shí)報(bào)告,例如帶有進(jìn)度條的GUI或在每次測(cè)試被調(diào)用時(shí)顯示點(diǎn)(“。”)的文本報(bào)告程序(如下所述),則ITestListener是您的最好的選擇。
1.2.1-日志偵聽器
這是一個(gè)顯示“。”的偵聽器。對(duì)于每個(gè)通過的測(cè)試,對(duì)于每個(gè)失敗,都為“ F”,對(duì)于每個(gè)跳過均為“ S”:
/***@author北京-宏哥
*
* Java自動(dòng)化測(cè)試框架-10 - TestNG之 測(cè)試結(jié)果篇
*
* 2019年11月9日*/
public class DotTestListener extendsTestListenerAdapter {private int m_count = 0;
@Overridepublic voidonTestFailure(ITestResult tr) {
log("F");
}
@Overridepublic voidonTestSkipped(ITestResult tr) {
log("S");
}
@Overridepublic voidonTestSuccess(ITestResult tr) {
log(".");
}private voidlog(String string) {
System.out.print(string);if (++m_count % 40 == 0) {
System.out.println("");
}
}
}
在此示例中,我選擇擴(kuò)展TestListenerAdapter,該方法使用空方法實(shí)現(xiàn)ITestListener,因此我不必從我不感興趣的接口中覆蓋其他方法。您可以根據(jù)需要直接實(shí)現(xiàn)該接口。
這是我調(diào)用TestNG來使用此新偵聽器的方法:
java -classpath testng.jar;%CLASSPATH% org.testng.TestNG -listener org.testng.reporters.DotTestListener test\testng.xml
和輸出:
........................................
........................................
........................................
........................................
........................................
.........................===============================================TestNG JDK1.5Total tests run:226, Failures: 0, Skips: 0
===============================================
請(qǐng)注意,當(dāng)您使用-listener時(shí),TestNG將自動(dòng)確定您要使用的偵聽器的類型。
1.2.2-日志記者
該org.testng.IReporter接口只有一個(gè)方法:
public void generateReport(List suites, String outputDirectory)
當(dāng)所有套件都已運(yùn)行時(shí),TestNG將調(diào)用此方法,您可以檢查其參數(shù)以訪問剛剛完成的運(yùn)行中的所有信息。
1.2.3-JUnitReports
TestNG包含一個(gè)偵聽器,該偵聽器獲取TestNG結(jié)果并輸出一個(gè)XML文件,然后可以將其饋送到JUnitReport。 這是一個(gè)示例,以及創(chuàng)建此報(bào)告的ant任務(wù):
注意:JDK 1.5和JUnitReports當(dāng)前不兼容,無法使用框架版本,因此您需要指定“ noframes”才能使其正常工作。
1.2.4-Reporter API
如果需要日志應(yīng)在生成的HTML報(bào)告中顯示的消息,則可以使用org.testng.Reporter類:
Reporter.log (“已呼叫M3” );
? ? ?
1.2.5-XML報(bào)告
TestNG提供了一個(gè)XML報(bào)告程序,用于捕獲JUnit報(bào)告中不提供的TestNG特定信息。當(dāng)用戶的測(cè)試環(huán)境需要使用JUnit格式無法提供的具有TestNG特定數(shù)據(jù)的XML結(jié)果時(shí),此功能特別有用。記者可以通過使用命令行注入TestNG的-reporter。
這是一個(gè)示例用法:-reporter org.testng.reporters.XMLReporter:generateTestResultAttributes = true,generateGroupsAttribute = true。
下表詳細(xì)介紹了可以傳遞的所有選項(xiàng)。確保使用:
: -將報(bào)告者名稱與其屬性分開
= -分隔屬性的鍵/值對(duì)
, -分隔多個(gè)鍵/值對(duì)
以下是此類報(bào)告器的輸出示例:
... Removed 22 stack frames]]>
該報(bào)告程序與其他默認(rèn)偵聽器一起注入,因此默認(rèn)情況下您可以獲得這種類型的輸出。偵聽器提供了一些屬性,可以對(duì)報(bào)告器進(jìn)行調(diào)整以滿足您的需求。下表包含這些屬性的列表,并附有簡(jiǎn)短說明:
PropertyCommentDefault 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??element. For example, you will get??for false and??for true.
false
generateGroupsAttribute
A boolean indicating if a?groups?attribute should be generated for the??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??elements.
false
generateTestResultAttributes
A boolean indicating if an??tag should be generated for each??element, containing the test result attributes (See?ITestResult.setAttribute()?about setting test result attributes). Each attribute?toString()?representation will be written in a??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??element.
true
generateDependsOnGroups
Enable/disable the generation of a?depends-on-groups?attribute for the??element.
true
為了配置此報(bào)告程序,可以在命令行中使用-reporter選項(xiàng),也可以將Ant?任務(wù)與嵌套的元素一起使用。對(duì)于其中的每個(gè),您都必須指定org.testng.reporters.XMLReporter類。請(qǐng)注意,您無法配置內(nèi)置報(bào)告器,因?yàn)樵搱?bào)告器僅使用默認(rèn)設(shè)置。如果只需要
帶有自定義設(shè)置的XML報(bào)告,則必須使用兩種方法之一手動(dòng)添加它并禁用默認(rèn)偵聽器。
1.2.6-TestNG退出代碼
當(dāng)TestNG完成執(zhí)行時(shí),它將退出并返回代碼。
可以檢查此返回碼以了解故障的性質(zhì)(如果有的話)。
下表總結(jié)了TestNG當(dāng)前使用的不同退出代碼。
FailedWithinSuccessSkippedFailedStatus CodeRemarks
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.-小結(jié)
好了,今天關(guān)于TestNG之測(cè)試結(jié)果,就分享到這里。
總結(jié)
以上是生活随笔為你收集整理的java自动化测试报告_Java自动化测试框架-10 - TestNG之测试结果篇(详细教程)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hook java_Java 实现 Ho
- 下一篇: java calendar字符串显示_J