在TestNG Introduction章节中,我们遇到了TestNG Framework中使用的不同注释,但到目前为止我们只使用了三个(Before,After和Test)。尽管这些是最常用的注释,但是谁知道你将使用你的框架走多远,并且可能喜欢使用其他有用的TestNG注释。
在此之前,我希望您对TestNG中的Annotations层次结构或Annotations级别提出一个小想法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <suite>
<test>
<classes>
<method>
<test>
</method>
</classes>
</test>
</suite> |
它说@Test是这里最小的注释。@Method将在执行@Test之前和之后执行。同样的方式@Class将在执行@Method之前和之后执行,依此类推。
现在通过以下示例,您将很容易清楚。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | package automationFramework;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Sequencing {
@Test
public void testCase1() {
System.out.println("This is the Test Case 1");
}
@Test
public void testCase2() {
System.out.println("This is the Test Case 2");
}
@BeforeMethod
public void beforeMethod() {
System.out.println("This will execute before every Method");
}
@AfterMethod
public void afterMethod() {
System.out.println("This will execute after every Method");
}
@BeforeClass
public void beforeClass() {
System.out.println("This will execute before the Class");
}
@AfterClass
public void afterClass() {
System.out.println("This will execute after the Class");
}
@BeforeTest
public void beforeTest() {
System.out.println("This will execute before the Test");
}
@AfterTest
public void afterTest() {
System.out.println("This will execute after the Test");
}
@BeforeSuite
public void beforeSuite() {
System.out.println("This will execute before the Test Suite");
}
@AfterSuite
public void afterSuite() {
System.out.println("This will execute after the Test Suite");
}
} |
上面代码的输出将是这样的:
很明显,@ Suite注释是第一个,也是最后一个。然后是@Test,然后是@Class。现在,如果你注意到,@ Meethod已经执行了两次。由于@Test是类中的一个方法,因此@Method将始终为每个@Test方法执行。
测试用例分组
' Groups '是TestNG的另一个注释,可用于执行多个测试。假设您有百辆级车辆的测试,其中包括十种汽车方法,十种滑板车方法等。您可能希望批量运行所有摩托车测试。而且你希望所有人都在一个测试套件中。在分组的帮助下,您可以轻松克服这种情况。
怎么做…
1)为Car创建两种方法,为Scooter创建两种方法,为Car&Sedan Car创建一种方法。
2)使用(groups = {“Group Name”})分别对它们进行分组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | package automationFramework;
import org.testng.annotations.Test;
public class Grouping {
@Test (groups = { "Car" })
public void Car1() {
System.out.println("Batch Car - Test car 1");
}
@Test (groups = { "Car" })
public void Car2() {
System.out.println("Batch Car - Test car 2");
}
@Test (groups = { "Scooter" })
public void Scooter1() {
System.out.println("Batch Scooter - Test scooter 1");
}
@Test (groups = { "Scooter" })
public void Scooter2() {
System.out.println("Batch Scooter - Test scooter 2");
}
@Test (groups = { "Car", "Sedan Car" })
public void Sedan1() {
System.out.println("Batch Sedan Car - Test sedan 1");
}
} |
3)像这样创建一个testng xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <suite name="Suite">
<test name="Practice Grouping">
<groups>
<run>
<include name="Car" />
</run>
</groups>
<classes>
<class name="automationFramework.Grouping" />
</classes>
</test>
</suite> |
4) 右键单击testng.xml文件运行测试,然后选择 Run As > TestNG Suite。 在TestNg控制台中输出将如下所示:
注意: 我们刚刚从xml调用了'Car'组,它也执行了Sedan Car的测试,因为我们在宣布轿车组时也提到了'Car'。
团体俱乐部也是可能的,看看下面的xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <suite name="Suite">
<test name="Practice Grouping">
<groups>
<define name="All">
<include name="Car"/>
<include name="Scooter"/>
</define>
<run>
<include name="All"/>
</run>
</groups>
<classes>
<class name="automationFramework.Grouping" />
</classes>
</test>
</suite> |
您可以看到我们创建了一个名为“All”的新组,并在其中包含其他组。然后简单地调用新创建的组来执行。输出将是这样的:
依赖性测试
有时,您可能需要以特定顺序调用测试用例中的方法,或者您希望在方法之间共享某些数据和状态。TestNG支持这种依赖,因为它支持在测试方法之间声明显式依赖关系。
TestNG允许您通过以下方式指定依赖项:
- 在@Test注释中使用属性dependsOnMethods
- 在@Test注释中使用属性dependsOnGroups。
看看下面的例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package automationFramework;
import org.testng.annotations.Test;
public class Dependent {
@Test (dependsOnMethods = { "OpenBrowser" })
public void SignIn() {
System.out.println("This will execute second (SignIn)");
}
@Test
public void OpenBrowser() {
System.out.println("This will execute first (Open Browser)");
}
@Test (dependsOnMethods = { "SignIn" })
public void LogOut() {
System.out.println("This will execute third (Log Out)");
} |
输出将是这样的: