testng依赖_TestNG依赖关系–
DependOnMethods,dependsOn。。。
testng依赖
Sometimes we want our test cas to run in specific order. One of the very common examples is when we want to run test cas for CRUD operations. So we want to make sure that test data is inrted 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;
stng.annotations.Test;
public class TestNGDependencyExample {
@Test
public void inrt() {
System.out.println("inrting demo data");
}
@Test
public void lect() {
System.out.println("lecting 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
雨露计划
inrting demo data
lecting demo data
updating demo data
This is not what we want our test cas 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 u dependsOnMethods to tell TestNG which methods this test is dependent on, so tho methods should be executed before this method.
喷空
更好的⽅法是使⽤dependsOnMethods来告诉TestNG该测试依赖于哪些⽅法,因此这些⽅法应在此⽅法之前执⾏。
Below is our updated test class where we are creating the order of execution – inrt, lect, update and delete.
下⾯是我们更新的测试类,我们在其中创建执⾏顺序–插⼊,选择,更新和删除。
package com.journaldev.dependency;
stng.annotations.Test;
public class TestNGDependencyExample {
@Test
public void inrt() {
System.out.println("inrting demo data");
}
@Test(dependsOnMethods="inrt")
public void lect() {
System.out.println("lecting demo data");
}
@Test(dependsOnMethods="lect")
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:
测试运⾏的新输出为:
inrting demo data
lecting 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= {"inrt","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”. The methods depend on “pre-tests” group. We also have a cleanup method that depends on the “tests” group.
让我们来看⼀个复杂的⽰例,其中我们的测试依赖于⽅法和组。 所有较早定义的⽅法都是名为“测试”的组的⼀部分。 这些⽅法取决
于“预测试”组。 我们还有⼀种清理⽅法,具体取决于“测试”组。
package com.journaldev.dependency;
stng.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 inrt() {
System.out.println("inrting demo data");
}
@Test(dependsOnMethods = "inrt", groups = "tests")
public void lect() {
System.out.println("lecting demo data");
}
@Test(dependsOnMethods = "lect", groups = "tests")
public void update() {
System.out.println("updating demo data");
}
@Test(dependsOnMethods = { "inrt", "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
inrting demo data
lecting 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 u 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;
stng.annotations.Test;
public class TestNGDependencyXMLExample {
@Test(groups = "pre-tests")
public void init() {
System.out.println("init resources");
}
@Test(groups = "tests")
鱼火锅的做法public void inrt() {
System.out.println("inrting demo data");
}
@Test(groups = "tests")
public void lect() {
System.out.println("lecting 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 "testng/testng-1.0.dtd">
<suite name="TestNG XML Dependency Test Suite" time-out="300">
<test name="TestNGXML Dependency Test" verbo="2" time-out="500">
<groups>
<dependencies>
<group depends-on="pre-tests" name="tests"></group>
<group depends-on="tests" name="post-tests"></group>
</dependencies>
</groups>
<class>
<class
name="com.journaldev.dependency.TestNGDependencyXMLExample">
<methods>
<include name="init"></include>
<include name="cleanup"></include>
<include name="inrt" invocation-numbers="1"></include>
<include name="lect" invocation-numbers="2"></include>
<include name="update" invocation-numbers="3"></include>
<include name="delete" invocation-numbers="4"></include>
</methods>
</class>
</class>
</test>
</suite>
摘要 (Summary)
We looked into creating order of execution of test methods and groups using dependsOnMethods an
d dependsOnGroups properties of Test annotation. We also learned how to achieve the same thing in XML suite file.
我们研究了使⽤Test批注的dependsOnMethods和dependsOnGroups属性来创建测试⽅法和组的执⾏顺序。 我们还学习了如何在XML套件⽂件中实现相同的功能。
. 下载⽰例代码。
testng依赖