多次测试
有些情况下,您希望将测试数量放在单个测试类中,并且喜欢单次运行所有测试。在TestNG'@ Test '注释的帮助下,我们可以在单个TestNG文件中执行多个测试。
以一个testng类为例,测试四种不同的测试,并在控制台上打印测试序列。
怎么做…
1)按Ctrl + N,在TestNG类别下选择“ TestNG Class ” ,然后单击Next。
要么
右键单击Test Case文件夹,转到TestNG并选择“ TestNG Class ”。
2)如果您的项目已设置并且在创建TestNG类之前选择了“测试用例”文件夹,则源文件夹和包名称将预先填充在表单上。将类名设置为“ TestNG ”。
保持其余设置不变,不要立即检查“ @BeforeMethod ”,“ @ AfterMethod ”并单击“ 完成”。而已。
3)默认情况下,新类只有一个@Test方法。自己添加两个方法,并将相应的代码放在方法中。代码如下:
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 |
package automationFramework;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
public class MultipleTest {
public WebDriver driver;
@Test
public void One() {
System.out.println("This is the Test Case number One");
}
@Test
public void Two() {
System.out.println("This is the Test Case number Two");
}
@Test
public void Three() {
System.out.println("This is the Test Case number Three");
}
@Test
public void Four() {
System.out.println("This is the Test Case number Four");
}
} |
这将使您只需一个testng类就可以执行所有四个测试。看看输出。
注意: 默认情况下,@ Test注释的方法按字母顺序执行。查看下一个主题,了解如何确定@Test的优先级。
排序和优先排序
如果希望按顺序执行方法,则需要使用' priority '参数。 参数是修改注释功能的关键字。
让我们采用相同的上述示例,并按正确的顺序执行所有@Test方法。只需为从0(零)开始的所有@Test方法分配优先级。
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 |
package automationFramework;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
public class MultipleTest {
public WebDriver driver;
@Test(priority = 0)
public void One() {
System.out.println("This is the Test Case number One");
}
@Test(priority = 1)
public void Two() {
System.out.println("This is the Test Case number Two");
}
@Test(priority = 2)
public void Three() {
System.out.println("This is the Test Case number Three");
}
@Test(priority = 3)
public void Four() {
System.out.println("This is the Test Case number Four");
}
} |
注意: TestNG将执行具有最低优先级值的@Test注释,直到最大值。
以上输出:
跳过测试用例
想想你需要从你的testng类中跳过一个或多个@Test的情况。在testng中,您可以通过将'enabled'参数设置为'false'来轻松地处理这种情况,例如:
@Test(启用= false)
要在单个注释中使用两个或更多参数,请使用逗号分隔它们:
@Test(priority = 3,enabled = false)
再次使用相同的示例并为第三个测试设置值false。
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 |
package automationFramework;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
public class MultipleTest {
public WebDriver driver;
@Test(priority = 0)
public void One() {
System.out.println("This is the Test Case number One");
}
@Test(priority = 1)
public void Two() {
System.out.println("This is the Test Case number Two");
}
@Test(priority = 2, enabled = false)
public void Three() {
System.out.println("This is the Test Case number Three");
}
@Test(priority = 3)
public void Four() {
System.out.println("This is the Test Case number Four");
}
} |
以上示例的输出: