如何在 testNG 中使控制流从一个测试 class 流向另一个测试 class?
How do I make the control flow from one test class to another test class in testNG?
我有 4 个测试用例,我希望第一个测试用例从 Demo1.java 运行 然后 运行 其他 2 个测试用例从 Demo2.java 然后 return 回到 Demo1.java 执行最后的测试用例。
有什么办法可以实现吗?
感谢您的帮助。
- 我试过
priority
参数,但我想这只是检查同一测试中的优先级 class
这是一个示例代码 -
Demo1.java
class Demo1{
@Test
public void testOne(){ //This case should be executed first
}
@Test
public void testFour(){ //This case should be executed last
}
}
Demo2.java
class Demo2{
@Test
public void testTwo(){ // This case should be executed 2nd in the order
}
@Test
public void testThree(){ // This case should be executed 3rd in the order
}
}
是的,您可以在
的帮助下订购 Junit 测试用例
@FixMethodOrder(MethodSorters.JVM)
@TestMethodOrder(Alphanumeric.class)
@TestMethodOrder(OrderAnnotation.class)
@TestMethodOrder(TestCaseLengthOrder.class)
详情可以关注这个linkclick for more detail turtorial
您可以通过以下方法实现。
Class TEST1 扩展 Class TEST2 :
public class TEST1 extends TEST2{
@Test(priority = 1)
public void testOne() {
System.out.println("Test method one");
}
@Test(priority = 4)
public void testTwo() {
System.out.println("Test method two");
}
}
Class 测试 2:
@Test(priority = 2)
public void testThree() {
System.out.println("Test three method in Inherited test");
}
@Test(priority = 3)
public void testFour() {
System.out.println("Test four method in Inherited test");
}
@dependsOnGroups 和 @dependsOnMethods 注释的组合提供了跨 classes 和TestNG 中的方法。
- @dependsOnGroups 将有助于在 classes
之间实施排序
- @dependsOnMethods 将有助于在 class
内实施排序
这是使用@dependsOnGroups 和@dependsOnMethods 的工作示例:
场景:
- 有 2 个测试 classes - ATest.java, BTest.java
- ATest 有 2 个测试方法 - A1、A2
- BTest 有 2 种测试方法 - B1、B2
- 执行顺序必须是:A1、B1、B2、A2
ATest.java
public class ATest.java {
@Test(groups = "group-a1")
public void A1() {
System.out.println("Test A1: Runs before B1");
}
@Test(dependsOnGroups = "group-b2")
public void A2() {
System.out.println("Test A2: Runs after B2");
}
}
BTest.java
public class BTest {
@Test(dependsOnGroups = "group-a1")
public void B1() {
System.out.println("Test B1: Runs after A1");
}
@Test(dependsOnMethods = "B1", groups = "group-b2")
public void B2() {
System.out.println("Test B2: Runs after B1");
}
}
注:
仅使用 @dependsOnMethods 注释即可实现上述排序。
但是,不建议在 class 中使用 @dependsOnMethods,因为这将强制要求每个测试用例方法在整个套房。
@dependsOnGroups 的使用将允许测试命名的灵活性,并使跨 classes 实现排序变得容易,没有麻烦。
更多信息:
我有 4 个测试用例,我希望第一个测试用例从 Demo1.java 运行 然后 运行 其他 2 个测试用例从 Demo2.java 然后 return 回到 Demo1.java 执行最后的测试用例。
有什么办法可以实现吗? 感谢您的帮助。
- 我试过
priority
参数,但我想这只是检查同一测试中的优先级 class
这是一个示例代码 -
Demo1.java
class Demo1{
@Test
public void testOne(){ //This case should be executed first
}
@Test
public void testFour(){ //This case should be executed last
}
}
Demo2.java
class Demo2{
@Test
public void testTwo(){ // This case should be executed 2nd in the order
}
@Test
public void testThree(){ // This case should be executed 3rd in the order
}
}
是的,您可以在
的帮助下订购 Junit 测试用例@FixMethodOrder(MethodSorters.JVM)
@TestMethodOrder(Alphanumeric.class)
@TestMethodOrder(OrderAnnotation.class)
@TestMethodOrder(TestCaseLengthOrder.class)
详情可以关注这个linkclick for more detail turtorial
您可以通过以下方法实现。
Class TEST1 扩展 Class TEST2 :
public class TEST1 extends TEST2{
@Test(priority = 1)
public void testOne() {
System.out.println("Test method one");
}
@Test(priority = 4)
public void testTwo() {
System.out.println("Test method two");
}
}
Class 测试 2:
@Test(priority = 2)
public void testThree() {
System.out.println("Test three method in Inherited test");
}
@Test(priority = 3)
public void testFour() {
System.out.println("Test four method in Inherited test");
}
@dependsOnGroups 和 @dependsOnMethods 注释的组合提供了跨 classes 和TestNG 中的方法。
- @dependsOnGroups 将有助于在 classes 之间实施排序
- @dependsOnMethods 将有助于在 class 内实施排序
这是使用@dependsOnGroups 和@dependsOnMethods 的工作示例:
场景:
- 有 2 个测试 classes - ATest.java, BTest.java
- ATest 有 2 个测试方法 - A1、A2
- BTest 有 2 种测试方法 - B1、B2
- 执行顺序必须是:A1、B1、B2、A2
ATest.java
public class ATest.java {
@Test(groups = "group-a1")
public void A1() {
System.out.println("Test A1: Runs before B1");
}
@Test(dependsOnGroups = "group-b2")
public void A2() {
System.out.println("Test A2: Runs after B2");
}
}
BTest.java
public class BTest {
@Test(dependsOnGroups = "group-a1")
public void B1() {
System.out.println("Test B1: Runs after A1");
}
@Test(dependsOnMethods = "B1", groups = "group-b2")
public void B2() {
System.out.println("Test B2: Runs after B1");
}
}
注: 仅使用 @dependsOnMethods 注释即可实现上述排序。
但是,不建议在 class 中使用 @dependsOnMethods,因为这将强制要求每个测试用例方法在整个套房。
@dependsOnGroups 的使用将允许测试命名的灵活性,并使跨 classes 实现排序变得容易,没有麻烦。
更多信息: