目录
首先,我们必须理解什么是TestNG中的组,以及为什么要创建组?
那么,如何在TestNG中创建组呢?
什么时候@BeforeGroups注释的方法会相对于其他@BeforeXXX注释执行?
假设情景
Q -如果我们试图执行一个跨越多个类的组中的案例,该怎么办?
如果我们试图执行一个跨越多个测试标签的组中的用例,该怎么办
Q -如果我们在一个类中有两个@BeforeGroups注释的方法怎么办?
我们已经写了一篇完全不同的文章,通过示例详细解释了TestNG组。如果你想深入了解TestNG中的组,我们建议你阅读这篇文章。
有时,一些测试用例验证系统的某个功能,但在应用程序的不同位置或页面上,所以它们通常在其他类或包中。它将帮助我们从测试用例所属的组中理解测试用例的功能。
很简单我们必须使用TestNG的@Test注释的“groups”属性。请记住,一个测试用例也可以属于一个或多个组,如下所示。
package Test;
import org.testng.Assert;
import org.testng.annotations.Test;
public class CodekruTest {
@Test(groups = { "group1", "group2" })
public void test1() {
System.out.println("Executing test1");
Assert.assertTrue(true);
}
@Test(groups = { "group1" })
public void test2() {
System.out.println("Executing test2");
Assert.assertTrue(true);
}
}
现在,让我们回到@BeforeGroups注释。
默认情况下,TestNG将按照XML文件中的顺序运行我们的测试。因此,它总是首先执行@BeforeSuite和@BeforeTest注释的方法,然后它将转到class,只有这样它才会找到位于特定组中的测试并执行@BeforeGroups注释的方法。
让我们看一个例子。在这里,我们使用CodekruTestFirst类,并将尝试执行带@ BeforeGroups注释的方法沿着和@BeforeTest注释,以查看它们的执行顺序。
CodekruTestFirst.java
package Test;
import org.junit.BeforeClass;
import org.testng.Assert;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class CodekruTestFirst {
@BeforeSuite
public void beforeSuite() {
System.out.println("beforeSuite method executed");
}
@BeforeClass
public void beforeClass() {
System.out.println("beforeClass method executed");
}
@BeforeMethod()
public void beforeMethod() {
System.out.println("beforeMethod method executed");
}
@BeforeTest()
public void beforeTest() {
System.out.println("beforeTest method executed");
}
@BeforeGroups(groups = "group1")
public void beforeGroup() {
System.out.println("beforeGroup method executed");
}
@Test(groups = { "group1" })
public void test1() {
System.out.println("Executing test1 in CodekruTestFirst class");
Assert.assertTrue(true);
}
}
下面是运行group1组的XML文件。
运行上述XML文件后的输出
beforeGroup method executed
Executing test1 in CodekruTestFirst class
===============================================
codekru
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
在这里,我们可以看到TestNG只执行了beforeGroup()方法,因为@BeforeGroups annotated方法有与之关联的groups属性。所以,如果我们想检查@BeforeGroups annotation到其他annotation的执行顺序,我们必须将其他annotated方法与“groups”属性关联。
package Test;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class CodekruTestFirst {
@BeforeSuite(groups = "group1")
public void beforeSuite() {
System.out.println("beforeSuite method executed");
}
@BeforeClass(groups = "group1")
public void beforeClass() {
System.out.println("beforeClass method executed");
}
@BeforeMethod(groups = "group1")
public void beforeMethod() {
System.out.println("beforeMethod method executed");
}
@BeforeTest(groups = "group1")
public void beforeTest() {
System.out.println("beforeTest method executed");
}
@BeforeGroups(groups = "group1")
public void beforeGroup() {
System.out.println("beforeGroup method executed");
}
@Test(groups = { "group1" })
public void test1() {
System.out.println("Executing test1 in CodekruTestFirst class");
Assert.assertTrue(true);
}
}
我们将重新编译相同的XML文件,下面是它的输出:
beforeSuite method executed
beforeTest method executed
beforeClass method executed
beforeGroup method executed
beforeMethod method executed
Executing test1 in CodekruTestFirst class
===============================================
codekru
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
让我们再看一个类,“CodekruTestSecond”和上一个类“CodekruTestFirst”。现在,CodekruTestSecond类中的cases属于group 1,并拥有自己的@BeforeTest注释方法。
CodekruTestFirst.java
package Test;
import org.testng.Assert;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class CodekruTestFirst {
@BeforeGroups(groups = "group1")
public void beforeGroups() {
System.out.println("beforeGroups method executed in CodekruTestFirst class");
}
@BeforeTest(groups = "group1")
public void beforeTest() {
System.out.println("beforeTest method executed in CodekruTestFirst class");
}
@Test(groups = { "group1" })
public void test() {
System.out.println("Executing test1 in CodekruTestFirst class");
Assert.assertTrue(true);
}
}
CodekruTestSecond.java
package Test;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class CodekruTestSecond {
@BeforeTest(groups = "group1")
public void beforeTest() {
System.out.println("beforeTest method executed in CodekruTestSecond class");
}
@Test(groups = { "group1" })
public void test1() {
System.out.println("Executing test1 in CodekruTestSecond class");
Assert.assertTrue(true);
}
}
现在,让我们尝试执行group1组。
执行上述XML文件后的输出-
beforeTest method executed in CodekruTestFirst class
beforeTest method executed in CodekruTestSecond class
beforeGroups method executed in CodekruTestFirst class
Executing test1 in CodekruTestFirst class
Executing test1 in CodekruTestSecond class
===============================================
codekru
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================
在这里,我们可以看到@BeforeGroup注释的方法只执行了一次。
执行上述XML后的输出
beforeTest method executed in CodekruTestFirst class
beforeGroup method executed
Executing test1 in CodekruTestFirst class
beforeTest method executed in CodekruTestSecond class
Executing test1 in CodekruTestSecond class
在这里,我们希望你把注意力集中在执行的顺序上。
让我们修改CodekruTestFirst类,使其具有两个带@BeforeGroups注释的方法。
package Test;
import org.testng.Assert;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class CodekruTestFirst {
@BeforeGroups(groups = "group1")
public void beforeGroup1() {
System.out.println("beforeGroup1 method executed");
}
@BeforeGroups(groups = "group1")
public void beforeGroup2() {
System.out.println("beforeGroup2 method executed");
}
@BeforeTest(groups = "group1")
public void beforeTest() {
System.out.println("beforeTest method executed in CodekruTestFirst class");
}
@Test(groups = { "group1" })
public void test1() {
System.out.println("Executing test1 in CodekruTestFirst class");
Assert.assertTrue(true);
}
}
下面是执行group1组的XML文件。
执行上述XML文件后的输出
beforeTest method executed in CodekruTestFirst class
beforeGroup1 method executed
beforeGroup2 method executed
Executing test1 in CodekruTestFirst class
在这里,我们可以看到TestNG执行了两个带注释的方法。