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

1.-测试结果

1.1-成功,失败和断言

测试被认为是成功的,如果它不引发任何异常完成,还是它扔的预期异常(请参阅文档expectedExceptions属性上找到的@Test注释)。

您的测试方法通常由可能引发异常的调用或各种断言(使用Java“ assert”关键字)组成。“断言”失败将触发AssertionErrorException,这反过来会将方法标记为失败(如果未看到断言错误,请记住在JVM上使用-ea)。

这是一个示例测试方法:

/***@author北京-宏哥

*

* Java自动化测试框架-10 - TestNG之 测试结果篇

*

* 2019年11月9日*/@Testpublic voidverifyLastName() {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.*;//...

@Testpublic voidverify() {

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 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("");

}

}

}

在此示例中,我选择扩展TestListenerAdapter,该方法使用空方法实现ITestListener,因此我不必从我不感兴趣的接口中覆盖其他方法。您可以根据需要直接实现该接口。

这是我调用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

===============================================

请注意,当您使用-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” );

b2e34137148844a38a6136b35ad2dd6e.png     

4b5819839621b06cc8068d56690a1137.png

1.2.5-XML报告

TestNG提供了一个XML报告程序,用于捕获JUnit报告中不提供的TestNG特定信息。当用户的测试环境需要使用JUnit格式无法提供的具有TestNG特定数据的XML结果时,此功能特别有用。记者可以通过使用命令行注入TestNG的-reporter。

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

下表详细介绍了可以传递的所有选项。确保使用:

: -将报告者名称与其属性分开

= -分隔属性的键/值对

, -分隔多个键/值对

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

... Removed 22 stack frames]]>

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

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

为了配置此报告程序,可以在命令行中使用-reporter选项,也可以将Ant 任务与嵌套的元素一起使用。对于其中的每个,您都必须指定org.testng.reporters.XMLReporter类。请注意,您无法配置内置报告器,因为该报告器仅使用默认设置。如果只需要

带有自定义设置的XML报告,则必须使用两种方法之一手动添加它并禁用默认侦听器。

1.2.6-TestNG退出代码

当TestNG完成执行时,它将退出并返回代码。

可以检查此返回码以了解故障的性质(如果有的话)。

下表总结了TestNG当前使用的不同退出代码。

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.-小结

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值