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
相关推荐
1、testng测试报告模板BeautifulReport 2、testng.xml文件
Abbot是一种JUnit扩展框架,主要目的是使GUI组件实现编程隔离,它还提供了一种验证GUI行为的简易方法。TestNG-Abbot是一种测试框架,它为GUI组件的测试带来了新的活力。本文介绍使用TestNG-Abbot测试GUI过程中难度...
集成 Jenkins 和 TestNG 实现自助式自动化测试平台,测试可行,。
展示了与测试有关的折衷考虑,这样您就能在测试什么和怎么测试方面做出更好的决定。 介绍了TestNG,说明了它的目标和功能,并展示了如何在真实的环境中应用这些功能。 展示了如何集成TestNG和已有的代码、开发框架...
《Java测试新技术:TestNG和高级概念》通过针对有效测试Java应用程序以及围绕可测试性来设计应用程序和组件展示了这些有效的测试技术,并给出了每种测试方法的优点和不足,展示了解决常见问题的不同选择。《Java测试...
《Java测试新技术TestNG和高级概念》通过针对有效测试Java应用程序以及围绕可测试性来设计应用程序和组件展示了这些有效的测试技术,并给出了每种测试方法的优点和不足,展示了解决常见问题的不同选择。《Java测试新...
java+selenium+maven+testng自动化测试框架实例(实际项目)
适用于想了解TestNG测试的同学
开源自动化测试框架testng包,用于appium+java+android测试
spring集成TestNG与Mockito框架单元测试方法,方便学习者能快速上手如何单测,对自己的代码能够有质量保证。
httpclient testgn 接口测试
spring集成TestNG与Mockito框架单元测试方法
展示了高级技术:测试部分失败、工厂、依赖关系测试、远程调用、基于集群的测试服务器群等。 介绍了在Eclipse和IDE中安装TestNG插件。 包含了大量的代码示例。 无论您使用TestNG,JUnit或其他测试框架,本书提供的...
testng下一代测试框架
ant+selenium+testng自动化测试方案
展示了高级技术:测试部分失败、工厂、依赖关系测试、远程调用、基于集群的测试服务器群等。 介绍了在Eclipse和IDE中安装TestNG插件。 包含了大量的代码示例。 无论您使用TestNG,JUnit或其他测试框架,本书提供的...
展示了与测试有关的折衷考虑,这样您就能在测试什么和怎么测试方面做出更好的决定。 介绍了TestNG,说明了它的目标和功能,并展示了如何在真实的环境中应用这些功能。 展示了如何集成TestNG和已有的代码、开发框架...
讲解如何使用Selenium 和 TestNG 进行编程式测试
Java + IDEA +TestNG 集成 Allure生成一套可视化测试报告 关于 Allure2: 1. 支持多语言多框架集成:包括java的testng、junit单元测试框架,和python的pytest等框架集成,满足当前行业里主流测试开发工程师手撕的...
java测试新技术TestNG和高级概念 TestNG是一个测试框架,其灵感来自JUnit和NUnit,但引入了一些新的功能,使其功能更强大,使用更方便。TestNG是一个开源自动化测试框架;TestNG表示下一代(Next Generation的首字母)...