`
longgangbai
  • 浏览: 7340581 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

TestNG简单的学习(三)测试方法通过测试分组依赖实现

阅读更多

TestNG官方网站:

http://testng.org/doc/documentation-main.html

 

依赖测试的 文档:

5.7 - Dependencies

 

Sometimes, you need your test methods to be invoked in a certain order. Here are a few examples:

  • To make sure a certain number of test methods have completed and succeeded before running more test methods.
  • To initialize your tests while wanting this initialization methods to be test methods as well (methods tagged with @Before/After will not be part of the final report).

TestNG allows you to specify dependencies either with annotations or in XML.

5.7.1 - Dependencies with annotations

 

You can use the attributes dependsOnMethods or dependsOnGroups, found on the @Test annotation.

There are two kinds of dependencies:

  • Hard dependencies. All the methods you depend on must have run and succeeded for you to run. If at least one failure occurred in your dependencies, you will not be invoked and marked as a SKIP in the report.
  • Soft dependencies. You will always be run after the methods you depend on, even if some of them have failed. This is useful when you just want to make sure that your test methods are run in a certain order but their success doesn't really depend on the success of others. A soft dependency is obtained by adding "alwaysRun=true" in your @Test annotation.

Here is an example of a hard dependency:

@Test
public void serverStartedOk() {}
 
@Test(dependsOnMethods = { "serverStartedOk" })
public void method1()
{}

 

In this example, method1() is declared as depending on method serverStartedOk(), which guarantees that serverStartedOk() will always be invoked first.

 

You can also have methods that depend on entire groups:

 

@Test(groups = { "init" })
public void serverStartedOk() {}
 
@Test(groups = { "init" })
public void initEnvironment() {}
 
@Test(dependsOnGroups = { "init.* })
public void method1()
{}

 

In this example, method1() is declared as depending on any group matching the regular expression "init.*", which guarantees that the methods serverStartedOk() and initEnvironment() will always be invoked before method1().

 

Note: as stated before, the order of invocation for methods that belong in the same group is not guaranteed to be the same across test runs.

 

If a method depended upon fails and you have a hard dependency on it (alwaysRun=false, which is the default), the methods that depend on it are not marked as FAIL but as SKIP. Skipped methods will be reported as such in the final report (in a color that is neither red nor green in HTML), which is important since skipped methods are not necessarily failures.

 

Both dependsOnGroups and dependsOnMethods accept regular expressions as parameters. For dependsOnMethods, if you are depending on a method which happens to have several overloaded versions, all the overloaded methods will be invoked. If you only want to invoke one of the overloaded methods, you should use dependsOnGroups.

 

For a more advanced example of dependent methods, please refer to this article, which uses inheritance to provide an elegant solution to the problem of multiple dependencies.

By default, dependent methods are grouped by class. For example, if method b() depends on method a() and you have several instances of the class that contains these methods (because of a factory of a data provider), then the invocation order will be as follows:

a(1)
a(2)
b(2)
b(2)

TestNG will not run b() until all the instances have invoked their a() method.

This behavior might not be desirable in certain scenarios, such as for example testing a sign in and sign out of a web browser for various countries. In such a case, you would like the following ordering:

 

signIn("us")
signOut("us")
signIn("uk")
signOut("uk")

For this ordering, you can use the XML attribute group-by-instances. This attribute is valid either on <suite> or <test>:

  <suite name="Factory" order-by-instances="true">
or
  <test name="Factory" order-by-instances="true">

 

5.7.2 - Dependencies in XML

Alternatively, you can specify your group dependencies in the testng.xml file. You use the <dependencies> tag to achieve this:

<test name="My suite">
  <groups>
    <dependencies>
      <group name="c" depends-on="a  b" />
      <group name="z" depends-on="c" />
    </dependencies>
  </groups>
</test>

The <depends-on> attribute contains a space-separated list of groups.

 

在测试中通过分组依赖实现,

package com.easyway.testng;

import org.testng.annotations.Test;

/**
 * 、测试方法直接通过测试分组依赖的实现
 * @author longgangbai
 * 2013-11-19  下午2:54:17
 *
 */
public class TestGroupDependenciesDataTest {
	   

	@Test(groups = { "init" })  
	public void serverStartedOk() {
		System.out.println("===================serverStartedOk==============");
	}  

	   

	@Test(groups = { "init" })  
	public void initEnvironment() {
		System.out.println("===================initEnvironment==============");
	}  

	   

	@Test(dependsOnGroups = { "init.*" })  
	public void method1() {
		System.out.println("===================method1==============");
		
	} 


}

 

测试结果:

[TestNG] Running:
  C:\Users\Administrator\AppData\Local\Temp\testng-eclipse-385127487\testng-customsuite.xml

===================initEnvironment==============
===================serverStartedOk==============
===================method1==============
PASSED: initEnvironment
PASSED: serverStartedOk
PASSED: method1

===============================================
    Default test
    Tests run: 3, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

[TestNG] Time taken by org.testng.reporters.EmailableReporter2@8f57a: 10 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@14e3372: 10 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@de648: 10 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@f9aa66: 10 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@1b7a531: 0 ms

 

 

分享到:
评论

相关推荐

    testNG单元测试学习总结

    - **测试分组**: 支持将测试方法分组,便于管理和组织测试用例。 - **依赖性测试**: 允许指定测试方法之间的依赖关系,确保测试按正确顺序执行。 - **并行测试**: 可以同时执行多个测试方法或测试类,提高测试效率。...

    testng-6.7.jar TestNG依赖包

    TestNG是一个功能强大的Java测试框架,由Cédric Beust创建,它在JUnit的基础上进行了扩展,提供了更多高级功能,如并行测试、测试分组、配置方法、监听器、报告定制等。TestNG的"testng-6.7.jar"是这个框架的核心库...

    java测试新技术-TestNG和高级概念

    2. 测试方法(Test Method):与JUnit类似,TestNG允许开发者将方法标记为测试方法,通常通过使用注解(@Test)来实现。TestNG中的测试方法可以有多种类型,包括常规测试、配置方法、监听器等。 3. 注解(Annotation):...

    testng测试ecplise插件

    它提供了许多优于JUnit的功能,如并行测试、测试分组、依赖性管理等,使得测试工作更加高效和灵活。Eclipse作为流行的Java开发IDE,通过安装TestNG插件,可以很好地集成TestNG框架,便于开发人员编写、运行和管理...

    Java测试新技术:TestNG和高级概念

    2. **测试分组**:TestNG引入了测试分组的概念,可以将相关测试方法归类到不同的组中。这有助于组织和执行特定的测试集,例如,可以将回归测试和压力测试分别分组,便于进行有针对性的执行。 3. **依赖管理**:...

    testng-测试

    2. **分组测试**:通过分组功能,你可以将相关的测试用例组织在一起,便于执行或跳过特定的测试集。 3. **依赖性测试**:你可以设置测试用例之间的依赖关系,确保某个测试用例在另一个用例成功执行后才运行。 4. *...

    Maven+Selenium+TestNG+TestNG-xslt实现数据驱动测试框架

    本项目基于"Maven+Selenium+TestNG+TestNG-xslt"搭建了一个高效的数据驱动测试框架,以下将详细解释这个框架的核心组件及其实现方式。 首先,Maven是一个项目管理和综合工具,它帮助开发者管理依赖、构建项目、执行...

    testng junit 下一代测试框架

    2. **测试分组**:TestNG引入了测试分组的概念,允许你将相关测试逻辑组织在一起,便于管理、运行和跳过特定组的测试。 3. **注解丰富**:除了JUnit的`@Test`,TestNG还提供了如`@BeforeMethod`、`@AfterMethod`、`...

    TestNG框架使用

    TestNG是一款强大的测试框架,它提供了比JUnit更丰富的功能,尤其在并行测试、分组测试、依赖测试等方面表现突出。这篇博文将深入探讨如何使用TestNG进行单元测试和集成测试。 1. **TestNG基本概念** TestNG允许...

    Testng自动化测试框架实战详解,全网最新最全Testng自动化测试框架技术

    - **测试分组**:可以将测试方法归类到不同的组中,便于按需执行特定组的测试。 - **依赖性测试**:支持定义测试方法间的依赖关系,确保测试按正确的顺序执行。 - **并行执行**:能够在多线程或多进程环境下并行执行...

    Java测试新技术-TestNG

    5. 分组测试:通过注解@Groups可以将测试方法分组,在执行时可以选择运行特定组的测试。 6. 并行测试:TestNG支持多线程测试,可以配置执行测试时使用的线程数,从而提升测试效率。 7. 异常处理:TestNG能够记录...

    Java测试新技术TestNG和高级概念 (英文版 清晰pdf 可下载)

    3. **测试分组**:通过测试分组,你可以将相关的测试用例组织在一起,然后根据需要运行或跳过这些组。 4. **依赖注入**:TestNG支持依赖注入,可以方便地将依赖对象注入到测试类中,使得测试代码更简洁,更易于维护...

    基于java+testng+数据库的接口自动化测试.zip

    相比JUnit,TestNG提供了更丰富的功能,如并发测试、测试分组、依赖管理、报告生成等。在接口测试中,我们可以使用TestNG编写测试类和方法,定义测试套件,以及设置断言来验证接口响应是否符合预期。 **数据库**: ...

    TestNg测试ppt

    - **更多的灵活性**:TestNG 支持更灵活的测试组织方式,比如测试分组、依赖测试等。 - **依赖性测试**:允许用户指定测试方法之间的依赖关系,确保按照正确的顺序执行。 - **失败用例的重运行**:如果某个测试失败...

    Java测试新技术TestNG和高级概念.part2

    2.10 测试分组 2.11 代码覆盖率 2.12 本章小结 第3章 企业级测试 3.1 典型企业级场景 3.2 一个具体例子 3.3 测试实现 3.4 探索竞争消费者模式 3.5 重构的作用 3.6 本章小结 第4章 Java EE测试 4.1 容器内测试与...

    TestNG教程.pdf

    TestNG支持的注解非常多,例如用于标记测试方法的@Test、用于分组测试的@Groups以及用于依赖测试方法的@dependsOnMethods等。 TestNG的一个重要特性是它支持多线程测试,这意味着可以同时运行多个测试方法,从而...

    testng-6.9.7 源码

    1. **测试分组**:TestNG允许我们将测试用例分组,这样可以按照功能、环境或优先级进行分类执行。在源码中,我们可以看到`@Test(groups = {"group1", "group2"}`这样的注解,通过这些分组,我们可以控制哪些测试用例...

    selenium+testng页面测试

    6. **Ant**: Ant是Apache软件基金会的一个Java构建工具,虽然在现代项目中常常被Maven或Gradle取代,但在本案例中可能涉及到的是使用Ant来编译和运行TestNG测试用例。Ant通过XML配置文件定义构建任务,包括编译、...

    testNG单元测试Demo

    8. **分组测试**:利用`@Test(groups = {...})`进行测试分组,便于按需执行特定组的测试。 9. **测试监听器**:介绍`ITestListener`接口,自定义测试监听器以实现额外的日志记录、自定义报告等功能。 10. **测试参数...

Global site tag (gtag.js) - Google Analytics