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 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之测试结果,就分享到这里。

Java自动化测试框架-10 - TestNG之测试结果篇的更多相关文章

  1. Java自动化测试框架-11 - TestNG之annotation与并发测试篇 (详细教程)

    1.简介 TestNG中用到的annotation的快速预览及其属性. 2.TestNG基本注解(注释) 注解 描述 @BeforeSuite 注解的方法只运行一次,在当前suite所有测试执行之前执 ...

  2. 《手把手教你》系列基础篇(七十七)-java+ selenium自动化测试-框架设计基础-TestNG依赖测试- 上篇(详解教程)

    1.简介 今天主要是讲解和分享:TestNG中一个类中有多个测试方法的时候,多个测试方法的执行顺序或者依赖关系的问题.如果不用dependsOnMethods,testNG会自动根据@Test方法名称 ...

  3. 《手把手教你》系列基础篇(七十八)-java+ selenium自动化测试-框架设计基础-TestNG依赖测试- 中篇(详解教程)

    1.简介 上一篇讲解了依赖测试的各种方法,今天继续讲解依赖测试的方法,这一篇主要是讲解和分享通过xml文件配置组名依赖方法( 主要是测试组的用法).废话不说,直接上干货. 2.实例 测试组:一个组可包 ...

  4. 《手把手教你》系列基础篇(八十)-java+ selenium自动化测试-框架设计基础-TestNG依赖测试-番外篇(详解教程)

    1.简介 经过前边几篇知识点的介绍,今天宏哥就在实际测试中应用一下前边所学的依赖测试.这一篇主要介绍在TestNG中一个类中有多个测试方法的时候,多个测试方法的执行顺序或者依赖关系的问题.如果不用de ...

  5. Java自动化测试框架-01 - TestNG之入门篇 - 大佬的鸡肋,菜鸟的盛宴(详细教程)

    TestNG是什么? TestNG按照官方的定义: TestNG是一个测试框架,其灵感来自JUnit和NUnit,但引入了一些新的功能,使其功能更强大,使用更方便. TestNG是一个开源自动化测试框 ...

  6. Java自动化测试框架-02 - TestNG之理论实践 - 纸上得来终觉浅,绝知此事要躬行(详细教程)

    理论 TestNG,即Testing, NextGeneration,下一代测试技术,是一套根据JUnit 和NUnit思想而构建的利用注释来强化测试功能的一个测试框架,即可以用来做单元测试,也可以用 ...

  7. Java自动化测试框架-03 - TestNG之Test Group篇 - 我们一起组团打怪升级(详细教程)

    简介 其实这篇文章的group宏哥在上一篇中就提到过,但是就是举例一笔带过的,因此今天专门有一篇文章来讲解Group的相关知识.希望大家茅塞顿开 ,有着更进一步认识和了解测试组. 一.Test Gro ...

  8. Java自动化测试框架-04 - TestNG之Test Method篇 - 道法自然,法力无边(详细教程)

    简介 按照上一篇的计划,这一篇给小伙伴们分享一下测试方法. 一.设置参数 测试方法是可以带有参数的.每个测试方法都可以带有任意数量的参数,并且可以通过使用TestNG的@Parameters向方法传递 ...

  9. Java自动化测试框架-07 - TestNG之Factory篇 - 欢快畅游梦幻工厂(详细教程)

    简介 最近忙着装修博客园,没时间更新文章,今天终于抽出时间把上次写的一半的文章给写完了,新的博客园风格,希望大家喜欢.今天继续介绍testng的相关知识--工厂. 工厂允许你动态的创建测试.例如,假设 ...

随机推荐

  1. 整理一些大厂的开源平台及github,向他们看齐...

    有人苦恼,该如何突破技术的局限性... 有人羡慕,技术上你怎么懂得这么多... 有人哀叹,唉,我已经学不动了... 我的总结(纯属个人想法):身处IT,就得不断学习和积累,才不会被狠狠地甩在身后.什么 ...

  2. 设计一个A表数据抽取到B表的抽取过程

    原题如下: 解题代码如下: table1类: @Data @NoArgsConstructor @AllArgsConstructor public class table1{ private Str ...

  3. 色即是空,空即是色---java有关null的几件小事

    故事背景 ---摩诃般若波罗蜜多心经: 观自在菩萨,行深般若波罗蜜多时,照见五蕴皆空,度一切苦厄.舍利子,色不异空,空不异色:色即是空,空即是色.受想行识,亦复如是.舍利子,是诸法空相,不生不灭,不垢 ...

  4. HTML5 video视频字幕的使用和制作

    一.video支持视频格式: 以下是三种最常用的格式 1. ogg格式:带有Theora视频编码(免费)+Vorbis音频编码的Ogg文件(免费) 支持的浏览器:firefox.chrome.oper ...

  5. 分库分表(7)--- SpringBoot+ShardingSphere实现分库分表 + 读写分离

    分库分表(7)--- ShardingSphere实现分库分表+读写分离 有关分库分表前面写了六篇博客: 1.分库分表(1) --- 理论 2.分库分表(2) --- ShardingSphere(理 ...

  6. vue图片点击放大预览

    第一种:viewerjs使用介绍(PC.移动端都兼容) 1.先安装依赖 npm install v-viewer --save 2.main.js内引用并注册调用 //main.js import V ...

  7. DCL语句

    DCL语句我们现在默认使用的都是root用户,超级管理员,拥有全部的权限.但是,一个公司里面的数据库服务器上面可能同时运行着很多个项目的数据库.所以,我们应该可以根据不同的项目建立不同的用户,分配不同 ...

  8. 实验吧之【拐弯抹角】(url伪静态)

    题目地址:http://ctf5.shiyanbar.com/indirection/ 打开后给了源码 <?php // code by SEC@USTC echo '<html>& ...

  9. libevent::日志

    LibEvent 能记录内部的错误和警告日志,如果编译进日志支持功能,也会记录调试信息.默认情况下这些消息都是输出 到 stderr,你也可以通过提供自己的日志函数的方法来覆盖这种行为. 为了覆盖 L ...

  10. 全面认识nslookup命令及子命令