testng依赖

Sometimes we want our test cases to run in specific order. One of the very common examples is when we want to run test cases for CRUD operations. So we want to make sure that test data is inserted first, then it’s retrieved, updated and finally deleted.

有时我们希望测试用例按特定顺序运行。 一个非常常见的示例是当我们要为CRUD操作运行测试用例时。 因此,我们要确保先插入测试数据,然后再进行检索,更新和最后删除。

TestNG依赖示例–dependOnMethods (TestNG Dependency Example – dependsOnMethods)

Let’s say we have a test class for CRUD operations.

假设我们有一个针对CRUD操作的测试类。

package com.journaldev.dependency;import org.testng.annotations.Test;public class TestNGDependencyExample {@Testpublic void insert() {System.out.println("inserting demo data");}@Testpublic void select() {System.out.println("selecting demo data");}@Testpublic void update() {System.out.println("updating demo data");}@Testpublic void delete() {System.out.println("deleting demo data");}
}

Since there is no order defined by us, TestNG will execute them in the natural order of their names. So when we run test class, we will get following output.

由于我们没有定义任何顺序,因此TestNG将按照其名称的自然顺序执行它们。 因此,当我们运行测试类时,我们将获得以下输出。

deleting demo data
inserting demo data
selecting demo data
updating demo data

This is not what we want our test cases order to be. One of the quick and dirty ways is to change the method names so that they get executed in the order we want them to.

这不是我们希望测试用例排序的内容。 一种快速而肮脏的方法是更改​​方法名称,以使它们以我们希望的顺序执行。

The better way is to use dependsOnMethods to tell TestNG which methods this test is dependent on, so those methods should be executed before this method.

更好的方法是使用dependsOnMethods来告诉TestNG该测试依赖于哪些方法,因此这些方法应在此方法之前执行。

Below is our updated test class where we are creating the order of execution – insert, select, update and delete.

下面是我们更新的测试类,我们在其中创建执行顺序–插入,选择,更新和删除。

package com.journaldev.dependency;import org.testng.annotations.Test;public class TestNGDependencyExample {@Testpublic void insert() {System.out.println("inserting demo data");}@Test(dependsOnMethods="insert")public void select() {System.out.println("selecting demo data");}@Test(dependsOnMethods="select")public void update() {System.out.println("updating demo data");}@Test(dependsOnMethods="update")public void delete() {System.out.println("deleting demo data");}
}

The new output of test run is:

测试运行的新输出为:

inserting demo data
selecting demo data
updating demo data
deleting demo data

dependsOnMethods takes String array as argument. Below annotation on delete() method is also fine and produces the same result.

dependsOnMethods将String数组作为参数。 在delete()方法上的以下注释也可以,并且产生相同的结果。

@Test(dependsOnMethods= {"insert","update"})
public void delete() {System.out.println("deleting demo data");
}

TestNG依赖性测试–dependOnGroups (TestNG Dependency Tests – dependsOnGroups)

We can also specify if our test depends on any other groups. This is helpful when we have multiple methods and they are grouped together. So we can simply specify the group this test depends on, rather than specifying the huge list of all the methods.

我们还可以指定我们的测试是否取决于其他任何组。 当我们有多种方法并将它们组合在一起时,这将很有帮助。 因此,我们可以简单地指定此测试所依赖的组,而不是指定所有方法的庞大列表。

Let’s look at a little bit complex example where our tests are dependent on methods as well as groups. All the earlier defined methods are part of the group named “tests”. These methods depend on “pre-tests” group. We also have a cleanup method that depends on the “tests” group.

让我们来看一个复杂的示例,其中我们的测试依赖于方法和组。 所有较早定义的方法都是名为“测试”的组的一部分。 这些方法取决于“预测试”组。 我们还有一种清理方法,具体取决于“测试”组。

package com.journaldev.dependency;import org.testng.annotations.Test;public class TestNGDependencyExample {@Test(groups = "pre-tests")public void init() {System.out.println("init resources");}@Test(groups = "tests", dependsOnGroups = "pre-tests")public void insert() {System.out.println("inserting demo data");}@Test(dependsOnMethods = "insert", groups = "tests")public void select() {System.out.println("selecting demo data");}@Test(dependsOnMethods = "select", groups = "tests")public void update() {System.out.println("updating demo data");}@Test(dependsOnMethods = { "insert", "update" }, groups = "tests")public void delete() {System.out.println("deleting demo data");}@Test(dependsOnGroups = "tests")public void cleanup() {System.out.println("closing resources");}
}

The output of test execution is:

测试执行的输出为:

init resources
inserting demo data
selecting demo data
updating demo data
deleting demo data
closing resources

XML Suite中的TestNG依赖关系 (TestNG Dependency in XML Suite)

TestNG XML suite allows us to define dependencies between groups. If we have to define the methods invocation order then we can use invocation-numbers for methods element.

TestNG XML套件允许我们定义组之间的依赖关系。 如果必须定义方法的调用顺序,则可以对方法元素使用invocation-numbers

Let’s say we have a new class with almost same methods as earlier, but there is no dependency defined between methods and groups.

假设我们有一个新类,具有与以前几乎相同的方法,但是在方法和组之间没有定义依赖项。

package com.journaldev.dependency;import org.testng.annotations.Test;public class TestNGDependencyXMLExample {@Test(groups = "pre-tests")public void init() {System.out.println("init resources");}@Test(groups = "tests")public void insert() {System.out.println("inserting demo data");}@Test(groups = "tests")public void select() {System.out.println("selecting demo data");}@Test(groups = "tests")public void update() {System.out.println("updating demo data");}@Test(groups = "tests")public void delete() {System.out.println("deleting demo data");}@Test(groups = "post-tests")public void cleanup() {System.out.println("closing resources");}
}

Here is our TestNG XML suite that will define the order of execution of groups and methods.

这是我们的TestNG XML套件,它将定义组和方法的执行顺序。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="TestNG XML Dependency Test Suite" time-out="300"><test name="TestNGXML Dependency Test" verbose="2" time-out="500"><groups><dependencies><group depends-on="pre-tests" name="tests"></group><group depends-on="tests" name="post-tests"></group></dependencies></groups><classes><classname="com.journaldev.dependency.TestNGDependencyXMLExample"><methods><include name="init"></include><include name="cleanup"></include><include name="insert" invocation-numbers="1"></include><include name="select" invocation-numbers="2"></include><include name="update" invocation-numbers="3"></include><include name="delete" invocation-numbers="4"></include></methods></class></classes></test>
</suite>

摘要 (Summary)

We looked into creating order of execution of test methods and groups using dependsOnMethods and dependsOnGroups properties of Test annotation. We also learned how to achieve the same thing in XML suite file.

我们研究了使用Test批注的dependsOnMethodsdependsOnGroups属性来创建测试方法和组的执行顺序。 我们还学习了如何在XML套件文件中实现相同的功能。

GitHub Repository.GitHub Repository下载示例代码。

翻译自: https://www.journaldev.com/21389/testng-dependency-dependsonmethods-dependsongroups

testng依赖

testng依赖_TestNG依赖关系–DependOnMethods,dependsOnGroups相关推荐

  1. 一文讲尽门面日志slf4j和log4j、log4j2、logback依赖jar引用关系

    前言 之前都是使用SparkStreaming开发,最近打算学习一下Flink,就从官网下载了Flink 1.11,打算搞一个客户端,将程序提交在yarn上.因为Flink从1.7之后就不再提供Had ...

  2. 类继承和依赖注入的关系_管理类依赖关系:依赖关系注入,服务定位符和工厂简介,第1部分...

    类继承和依赖注入的关系 Let's face it: for good or bad, OOP has been actively drilling deep holes in the soil of ...

  3. 直接依赖,间接依赖,可选依赖,排除依赖,依赖冲突

    直接依赖 在本工程pom文件中配置的依赖,称为本工程的直接依赖. 间接依赖 本工程pom配置了依赖A,A又依赖B,则本工程也依赖B,B为本工程的间接依赖. 可选依赖 在依赖中配置<optiona ...

  4. 控制反转-依赖倒置-依赖注入

    控制反转:框架控制应用/组件 依赖倒置: 依赖注入:框架动态注入依赖关系到应用/组件 --------------------------------------------------------- ...

  5. java依赖_java 依赖、组合、聚合与继承

    java中类与类之间的关系 大部分的初学者只知道java中两个类之间可以是继承与被继承的关系,可是事实上,类之间的关系大体上存在五种-继承(实现).依赖.关联.聚合.组合. 接下来,简单的分析一下这些 ...

  6. Mvn : Maven的依赖管理 依赖冲突 解决思路

    文章目录 1.美图 2.依赖 3.依赖传递 4.依赖冲突 5.短路优先 6.声明优先 7.依赖排除 8.解决冲突 1.美图 视频参考:maven jar 包 冲突 的解决方式 2.依赖 Maven 核 ...

  7. Spark RDD 宽依赖窄依赖

    Spark RDD 宽依赖&窄依赖 1.窄依赖 2.宽依赖: 3.阶段的划分 4.宽依赖和窄依赖的作用: 1.窄依赖 每一个父RDD的Partition最多被子RDD的一个Partition使 ...

  8. 宽依赖和窄依赖_spark的宽依赖窄依赖

    1RDD的依赖关系及容错 1.1RDD的依赖关系 RDD的依赖关系分为两种:窄依赖(Narrow Dependencies)与宽依赖(Wide Dependencies,源码中称为ShuffleDep ...

  9. Spark宽依赖 窄依赖 Job Stage Executor Task 总结

    文章授权自: http://www.6aiq.com/article/1547041236424 宽依赖与窄依赖 窄依赖(narrow dependency)和宽依赖(wide dependency, ...

最新文章

  1. paip.http 404错误 的解决
  2. python就业方向及工资-Python的5大就业方向,薪资诱人前景好!
  3. 码长6075的qc-ldpc编译码的MATLAB误码率仿真
  4. 适合0基础的web开发系列教程-换行和水平线
  5. EndNote 20.1 for Win/MacOS 完美稳定版安装 重大更新,修复BUG听取用户反馈更易用
  6. golang的缓存io简单的使用
  7. C++:类对象的复制和赋值
  8. 安川机器人焊枪切换设定方法_【分享】焊接机器人的性能要求与系统构成
  9. Codeforce 1800Difficulty Graphs 20 questions
  10. PHP和原生JS实现九型人格在线测试(144题)
  11. override overload
  12. 让刷Q币者对爱机你无从下手
  13. java 成员变量存在哪_Java中成员变量、局部变量、全局变量、静态变量存在位置及初始化...
  14. ABAP 数据的基本输出Write简单用法
  15. JMeter插件之PerfMon监控服务器性能
  16. C++模板元编程(3)模板显示具体化
  17. idea提示“No suitable licenses associated with account balabala”
  18. 深度学习在三维点云上的应用(Deep Learning for 3D Point Clouds: A Survey)
  19. Windows 10系统用FileZilla Server 1.6.1搭建FTP服务器
  20. 网络-4 【http状态码、accept、Content-Type】

热门文章

  1. 【转】OCaml基础知识
  2. 四川专利代理机构列表
  3. 数据结构与算法(Python)第二天
  4. c#数据库事务锁类型
  5. day006bootstrap的简单学习 + 轮播图
  6. Java的JDK以及maven环境变量配置
  7. Gym 101246G Revolutionary Roads
  8. python基础7--socket
  9. MySQL 5.6 dump/load buffer pool实验
  10. C#服务启动以及服务指令