TestNG系列-第6章 测试结果

1 成功、失败和断言
当测试方法运行完之后,不会跑出任何异常或者跑出的是期望的异常(参考@Test注解的   expectedExceptions属性),则这个方法被认为是成功的。
测试方法通常由可以引发异常或者各种断言(assert)的调用来执行。断言"assert"会触发异常AssertionErrorException异常。这个异常会为这个方法标记为失败 (remember to use -ea on the JVM if you are not seeing the assertion errors).
下面是一个示例:
@Test
public void verifyLastName() {
  assert "Beust".equals(m_lastName) : "Expected name Beust, for" + m_lastName;
}

TestNG也包含Junit的断言类,可以让我们队复杂的对象执行断言。
import static org.testng.AssertJUnit.*;
//...
@Test
public void verify() {
  assertEquals("Beust", m_lastName);
}


2 日志和结果
测试执行的结果存于文件index.html中。一个典型的例子 here .
也可以很容易的用监听器和Reporters生成自己的报告:
比如:当测试运行完成之后,希望生成一个pdf报告,如果没有必要实时接收测试结果通知,可以用IResporter。如果需要输出实时结果,如有GUI的进度条等,可以选择用ITestListner。

2.1 日志监听器
下面展示一个监听器,显示'.'表示成功,F表示失败,S表示忽略。

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 继承了 TestListenerAdapter ,它实现了接口 ITestListener  ,这样我们就不需要重写接口的其他方法了。下面是调用这个新的Listner的命令:
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
===============================================


2.2 日志报告(Logging Reporters)
org.testng.IReporter接口只有一个方法:
public void generateReport(List<ISuite> suites, String outputDirectory)
当所有的测试集运行完之后这个方法会被调用。可以通过检查它的参数访问所有刚才运行完的测试的信息。

2.3 JunitReporters

TestNG有一个可以接受TestNG的结果并输出XML文件的监听器,输出的XML文件可以用于JunitReporter。示例 Here 。创建报告的Ant task如下:
<target name="reports"> <junitreport todir="test-report"> <fileset dir="test-output"> <include name="*/*.xml"/> </fileset> <report
<target name="reports">
  <junitreport todir="test-report">
    <fileset dir="test-output">
      <include name="*/*.xml"/>
    </fileset>
 
    <report format="noframes"  todir="test-report"/>
  </junitreport>
</target>
2.4 结果报告API
如果需要将日志信息展现在结果的Html报告中,可以使用类   org.testng.Reporter
   Reporter.log( "M3 WAS CALLED" );

2.5 XML报告

TestNG提供XML报告,XML报告可以收集在JUnit报告中不可获得的信息。当用户的测试环境需要查看TestNG中特别XML的结果而Junit形式的报告不能提供的时候,特别有用。下面是这种类似报告(reporter)的展示:
<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>

这个reporter和其他默认的监听器一起注入,因此默认情况下,我们可以获得这种类型的输出。监听器可以提供一些属性,这些属性可以调整reporter以满足我们的需求。下面的表格包含了这些属性的列表的解释。

属性 说明 默认值
outputDirectory 说明XML文件输出的路径 The TestNG output directory
timestampFormat 指定reporter生成的日期的格式 yyyy-MM-dd'T'HH:mm:ss'Z'
fileFragmentationLevel 一个整数,取值:1,2或者3.表示生成XML文件的方法:
1- 在一个文件中生成所有的结果
2- 每个测试集生成不同的XML文件,所有文件通过主文件相关联。
3- 和2类似,增加了不同的测试用例的文件。
1
splitClassAndPackageNames boolean值,用于指定用于生成在<class>元素生成类名的方式。如false时,可以  <class class="com.test.MyTest"> 。true时,  <class class="MyTest" package="com.test"> false
generateGroupsAttribute boolean值,表示是否为<test-method>生成一个分组属性。目的是提供一个直接的包含测试方法的检索组,而不必通过<group>元素。
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 boolean值表示是否应该为每个<test-method>生成标签<attributes>,包含了测试结果属性。
每个属性toString()的表示被描述在标签 <attribute name="[attribute name]"> 中。
false
stackTraceOutputMethod 指定生成异常的堆栈类型,有以下取值:
0-无堆栈跟踪(只有异常和信息)
1- 简短版本(short version)堆栈信息,只包含从栈顶起的几行
2 包含所有内部异常的完整的堆栈信息
3- 包含短和长版本的堆栈信息  both short and long stacktrace
2
generateDependsOnMethods 使用此属性启用/禁用“<test-method>元素”的方法属性的生成。
Use this attribute to enable/disable the generation of a depends-on-methods attribute for the <test-method> element.
true
generateDependsOnGroups 启用/禁用“depends-on-groups”的<test-method>元素的生成
Enable/disable the generation of a depends-on-groups attribute for the <test-method> element.
true
为了配置找个reporter,可以在命令行中使用-reporter选项,或者在Ant 的task中嵌入<reporter>元素。对于每个属性,需要制定类 org.testng.reporters.XMLReporter。需要注意的是,我们不能配置内嵌的reporter。因为这只能使用默认的设置。如果我们只需要带有自定义设置的XML格式的报告,必须手动的将它的两种方法之一,并且禁用默认的监听器。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值