您好,欢迎来到吉趣旅游网。
搜索
您的当前位置:首页Java自动化测试框架-10-TestNG之测试结果篇(详细教程)

Java自动化测试框架-10-TestNG之测试结果篇(详细教程)

来源:吉趣旅游网
Java⾃动化测试框架-10-TestNG之测试结果篇(详细教程)

转⾃: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\}

请注意,上⾯的代码使⽤静态导⼊,以便能够使⽤ 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 test\estng.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 suites, String outputDirectory)

当所有套件都已运⾏时,TestNG将调⽤此⽅法,您可以检查其参数以访问刚刚完成的运⾏中的所有信息。1.2.3-JUnitReports

TestNG包含⼀个侦听器,该侦听器获取TestNG结果并输出⼀个XML⽂件,然后可以将其馈送到JUnitReport。 这是⼀个⽰例,以及创建此报告的ant任务:

注意: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。下表详细介绍了可以传递的所有选项。确保使⽤:: -将报告者名称与其属性分开= -分隔属性的键/值对, -分隔多个键/值对

以下是此类报告器的输出⽰例:

java.lang.AssertionError

... Removed 22 stack frames ]]>

该报告程序与其他默认侦听器⼀起注⼊,因此默认情况下您可以获得这种类型的输出。侦听器提供了⼀些属性,可以对报告器进⾏调整以满⾜您的需求。下表包含这些属性的列表,并附有简短说明:

Property

outputDirectorytimestampFormatComment

A String indicating the directory where should the XML files be output.Specifies the format of date fields that are generated by this reporter

An integer having the values 1, 2 or 3, indicating the way that the XML files aregenerated:

fileFragmentationLevel 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.

Default valueThe TestNGoutput directoryyyyy-MM-dd'T'HH:mm:ss'Z'

1

This boolean specifies the way that class names are generated for the element.

splitClassAndPackageNamesFor example, you will get for false and package=\"com.test\"> for true.generateGroupsAttributeA boolean indicating if a groups attribute should be generated for the element. This feature aims at providing a straight-forward method of retrievingthe groups that include a test method without having to surf throughthe elements.

false

false

A boolean indicating if an tag should be generated for each element, containing the test result attributes (See ITestResult.setAttribute() about

generateTestResultAttributessetting test result attributes). Each attribute toString() representation will be written in

false

a tag.

Specifies the type of stack trace that is to be generated for exceptions and has thefollowing values:

stackTraceOutputMethod 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

generateDependsOnMethodsgenerateDependsOnGroupsUse this attribute to enable/disable the generation of a depends-on-methods attribute forthe element.

true

Enable/disable the generation of a depends-on-groups attribute for the element.true

为了配置此报告程序,可以在命令⾏中使⽤-reporter选项,也可以将 任务与嵌套的元素⼀起使⽤。对于其中的每个,您都必须指定org.testng.reporters.XMLReporter类。请注意,您⽆法配置内置报告器,因为该报告器仅使⽤默认设置。如果只需要带有⾃定义设置的XML报告,则必须使⽤两种⽅法之⼀⼿动添加它并禁⽤默认侦听器。1.2.6-TestNG退出代码

当TestNG完成执⾏时,它将退出并返回代码。可以检查此返回码以了解故障的性质(如果有的话)。下表总结了TestNG当前使⽤的不同退出代码。FailedWithinSuccessSkippedFailedStatus CodeNoNoNoNoYesYesYesYes

NoNoYesYesNoNoYesYes

NoYesNoYesNoYesNoYes

01234567

Passed testsFailed testsSkipped testsSkipped/Failed testsFailedWithinSuccess testsFailedWithinSuccess/Failed testsFailedWithinSuccess/Skipped testsFailedWithinSuccess/Skipped/Failed tests

Remarks

2.-⼩结

好了,今天关于TestNG之测试结果,就分享到这⾥。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- jqkq.cn 版权所有 赣ICP备2024042794号-4

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务