testng依赖_TestNG依赖关系–DependOnMethods,dependsOnGroups

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 {

	@Test
	public void insert() {
		System.out.println("inserting demo data");
	}
	
	@Test
	public void select() {
		System.out.println("selecting demo data");
	}
	
	@Test
	public void update() {
		System.out.println("updating demo data");
	}
	
	@Test
	public 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 {

	@Test
	public 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>
			<class
				name="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依赖

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值