如何在TestNG中实现从一个测试类控制流转到另一个测试类?

huangapple go评论73阅读模式
英文:

How do I make the control flow from one test class to another test class in testNG?

问题

这里是你要求的翻译内容:

我有4个测试用例,我希望第一个测试用例从Demo1.java运行,然后从Demo2.java运行另外2个测试用例,然后返回Demo1.java执行最后一个测试用例。

我是否有办法实现这一点?
感谢你的帮助。

  • 我尝试过priority参数,但我猜那只是检查同一个测试类中的优先级。

以下是示范代码 -

Demo1.java

class Demo1{

  @Test
  public void testOne(){ // 这个测试用例应该首先执行
  }

  @Test
  public void testFour(){ // 这个测试用例应该最后执行
  }

}

Demo2.java

class Demo2{

  @Test
  public void testTwo(){ // 这个测试用例应该第2个执行
  }

  @Test
  public void testThree(){ // 这个测试用例应该第3个执行
  }
}
英文:

I have 4 test cases and I want the first test case to run from Demo1.java and then run the other 2 test cases from Demo2.java and then return back to Demo1.java to execute the final test case.

Is there any way I can achieve this?
Thanks for your help.

  • I have tried priority parameter but I guess that just checks for the priorities in the same test class

Here is a sample code -

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
  }
}

答案1

得分: 3

以下是翻译的内容:

结合 @dependsOnGroups@dependsOnMethods 注解,提供了 TestNG 中跨类和跨方法进行测试序列化的最可扩展的方式。

  • @dependsOnGroups 将帮助在类之间实现序列化
  • @dependsOnMethods 将帮助在类内部实现序列化

下面是使用 @dependsOnGroups 和 @dependsOnMethods 的工作示例:

场景:

  • 有两个测试类 - ATest.java,BTest.java
  • ATest 有两个测试方法 - A1,A2
  • BTest 有两个测试方法 - B1,B2
  • 执行顺序必须为:A1,B1,B2,A2

ATest.java

public class ATest.java {

    @Test(groups = "group-a1")
    public void A1() {
        System.out.println("Test A1: 在 B1 之前运行");
    }

    @Test(dependsOnGroups = "group-b2")
    public void A2() {
        System.out.println("Test A2: 在 B2 之后运行");
    }
}

BTest.java

public class BTest {

    @Test(dependsOnGroups = "group-a1")
    public void B1() {
        System.out.println("Test B1: 在 A1 之后运行");
    }

    @Test(dependsOnMethods = "B1", groups = "group-b2")
    public void B2() {
        System.out.println("Test B2: 在 B1 之后运行");
    }
}

注意:
上述序列可以仅通过使用 @dependsOnMethods 注解来实现。

然而,不建议在类之间使用 @dependsOnMethods,因为这将使整个测试套件中的每个测试用例方法都必须具有唯一的名称。

使用 @dependsOnGroups 将允许在测试命名方面更加灵活,并且可以轻松实现类之间的序列化,而无需麻烦。


更多信息:

https://testng.org/doc/documentation-main.html

英文:

The combination of @dependsOnGroups and @dependsOnMethods annotations provides the most scalable way of test sequencing across classes and methods in TestNG.

  • @dependsOnGroups will help implement sequencing across classes
  • @dependsOnMethods will help implement sequencing within a class

Here is the working example using @dependsOnGroups and @dependsOnMethods:

Scenario:

  • There are 2 test classes - ATest.java, BTest.java
  • ATest has 2 test methods - A1, A2
  • BTest has 2 test methods - B1, B2
  • The sequence of execution must be: 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");
	}
}

Note:
The above sequencing can be implemented by using just the @dependsOnMethods annotation only.

However, it is not recommended to use @dependsOnMethods across classes, as it would make it mandatory to have unique name for each test case method in the entire suite.

The use of @dependsOnGroups will allow flexibility in test naming, and make it easy to implement sequencing across classes, without hassle.


More information:

https://testng.org/doc/documentation-main.html

答案2

得分: 2

是的,你可以通过以下方式来对 JUnit 测试用例进行排序:

@FixMethodOrder(MethodSorters.JVM)
@TestMethodOrder(Alphanumeric.class)
@TestMethodOrder(OrderAnnotation.class)
@TestMethodOrder(TestCaseLengthOrder.class)

有关详细信息,你可以参考这个链接:点击获取更多详细教程

英文:

Yes you can order Junit test Case by help of

@FixMethodOrder(MethodSorters.JVM)
@TestMethodOrder(Alphanumeric.class)
@TestMethodOrder(OrderAnnotation.class)
@TestMethodOrder(TestCaseLengthOrder.class)

for details you can follow this link click for more detail turtorial

答案3

得分: 0

你可以通过以下方法实现。

类 TEST1 继承自类 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");
    }
}

类 TEST2:

public class TEST2 {
    @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");
    }
}
英文:

You can achieve by following below approach.

Class TEST1 extends 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 TEST2 :

@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");
}

huangapple
  • 本文由 发表于 2020年9月6日 01:51:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63756913.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定