简体   繁体   English

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

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

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.我有 4 个测试用例,我希望第一个测试用例从 Demo1.java 运行,然后从 Demo2.java 运行其他 2 个测试用例,然后返回到 Demo1.java 以执行最终的测试用例。

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我已经尝试过priority参数,但我想这只是检查同一测试类中的优先级

Here is a sample code -这是一个示例代码 -

Demo1.java 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 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
  }
}

The combination of @dependsOnGroups and @dependsOnMethods annotations provides the most scalable way of test sequencing across classes and methods in TestNG. @dependsOnGroups@dependsOnMethods注释的组合提供了 TestNG 中跨类和方法的测试排序的最具扩展性的方式。

  • @dependsOnGroups will help implement sequencing across classes @dependsOnGroups将有助于实现跨类的排序
  • @dependsOnMethods will help implement sequencing within a class @dependsOnMethods将有助于在类中实现排序

Here is the working example using @dependsOnGroups and @dependsOnMethods:这是使用@dependsOnGroups 和@dependsOnMethods 的工作示例:

Scenario:设想:

  • There are 2 test classes - ATest.java, BTest.java有 2 个测试类 - ATest.java, BTest.java
  • ATest has 2 test methods - A1, A2 ATest 有 2 种测试方法 - A1、A2
  • BTest has 2 test methods - B1, B2 BTest 有 2 种测试方法 - B1、B2
  • The sequence of execution must be: A1, B1, B2, A2执行顺序必须是: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.注意:上述排序可以仅通过使用@dependsOnMethods注释来实现。

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.但是,不建议跨类使用@dependsOnMethods ,因为它会强制要求整个套件中的每个测试用例方法都具有唯一的名称。

The use of @dependsOnGroups will allow flexibility in test naming, and make it easy to implement sequencing across classes, without hassle.使用@dependsOnGroups将允许测试命名的灵活性,并可以轻松实现跨类的排序,没有麻烦。


More information:更多信息:

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

Yes you can order Junit test Case by help of是的,您可以在以下人员的帮助下订购 Junit 测试用例

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

for details you can follow this link click for more detail turtorial有关详细信息,您可以点击此链接单击以获取更多详细信息教程

You can achieve by following below approach.您可以通过以下方法实现。

Class TEST1 extends Class TEST2 : 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");
}

} }

Class TEST2 :类 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");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在硒webdriver中从一个java类(页面)到另一个TestNG类(test)调用下拉值? - How to call dropdown value from one java class(page) to another TestNG class(test) in selenium webdriver? 如何从TestNG的jar中使用测试类 - How to use a test class from a jar in TestNG 如何将参数从1类文件中的@Test方法传递到另一个类文件中的注释方法(testNG) - how to pass parameter from @Test method in 1 class file to a annotated method in another class file(testNG) 如何在TestNG框架中将值从一个类传递到另一个类 - How to pass values from one class to another class in TestNG framework 如何在另一个类中使用来自测试类的JOptionPane输入? - How do I use a JOptionPane input from a test class in another class? 如何告诉Maven和TestNG运行特定的测试类或suite.xml文件? - How do I tell Maven and TestNG to run either a specific test class or a suite.xml file? 如何使用junit或testng为Queue接口类创建简单的单元测试? - How do I create a simple unit test using junit or testng for a Queue interface class? 我如何测试这堂课? - How do I test this class? 如何将全局变量从一个测试类传递到另一个测试类并在 selenium 的下拉列表中选择该值? - How to pass the global variable from one Test class to another test class and select that value in a dropdown in selenium? 如何将 TestNG 中的驱动程序传递给第二次测试 class? - How to pass driver in TestNG to second test class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM