简体   繁体   English

仅在您的 class 之一完成测试后,如何在 class 级别并行性中运行您的测试

[英]How to run your tests in class level parallelism only after one of your class has finished with it's tests

I got stucked in rather unique problem and I'm sure some of you might have faced similar problem at some point.我陷入了一个相当独特的问题,我相信你们中的一些人可能在某些时候遇到过类似的问题。

So I have total 10 Test classes in my testNg suite and I want them to run in parallel.所以我的 testNg 套件中共有 10 个测试类,我希望它们并行运行。 This is easy but the problem is one of my class lets assume class Test A, not only sets up the test data but also tests some functionality of the application, hence I can not use it under BeforeSuite or BeforeTest annotation.这很容易,但问题是我的 class 之一让我们假设 class 测试 A,不仅设置测试数据,还测试应用程序的某些功能,因此我不能在 BeforeSuite 或 BeforeTest 注释下使用它。 The Test data created via the execution of tests under this class is important for other test classes to run, hence I'm really scratching my head how to implement Class level parallelism in such a way that the parallel run only initiate after the tests under Tests A class finishes it's execution.通过在此 class 下执行测试创建的测试数据对于其他测试类的运行很重要,因此我真的很想知道如何以这样的方式实现 Class 级别并行性,即并行运行仅在测试下的测试后启动class 完成了它的执行。

Summary概括

Class Test A Class 测试A

public class TestsA {

   @BeforeClass
   public setupTestClass(){
   -----some code--------
  }
  @Test
  public test1(){
 ------some code---
}

I want this TestA class to run first and then instantiate other test classes in parallel.我希望这个 TestA class 先运行,然后并行实例化其他测试类。 Any help regarding this would be really appreciated.对此的任何帮助将不胜感激。

Thanks谢谢

By default all the Test methods will have a priority '0' value.默认情况下,所有测试方法都将具有优先级“0”值。 So if you want a specific test method to execute before all the other tests then you could set the priority as -1 for that particular Test.因此,如果您希望在所有其他测试之前执行特定的测试方法,那么您可以将该特定测试的优先级设置为 -1。 Lesser the priority first will be its execution in testng.次要优先级将是它在 testng 中的执行。 So test with priority as -1 precedes all the other tests with priority '0'.因此,优先级为 -1 的测试优先于所有其他优先级为“0”的测试。

Example Setting priority for a single Test method示例为单个测试方法设置优先级

public class ClassA {

@Test(priority = -1)
public void b(){
    System.out.println("Test1");
}

@Test
public void a(){
    System.out.println("Test2");
}

} }

Example 2: Setting priority for the entire class示例 2:为整个 class 设置优先级

@Test(priority = -1)
public class ClassA {


public void b(){
    System.out.println("Test1");
}


public void a(){
    System.out.println("Test2");
}

} }

Output For both the examples: Output 对于这两个示例:

Test1测试1

Test2测试2

I've found the solution for the above problem.我已经找到了解决上述问题的方法。 All I had to do is to have two different tests configured on my testng.xml suite.我所要做的就是在我的 testng.xml 套件上配置两个不同的测试。 1st test will have only Test class A and the other test will have other test classes having parallelism at class level.第一个测试将只有测试 class A,而另一个测试将有其他测试类在 class 级别具有并行性。 Since under 1st test there's only one class, the tests under that class will be executed first.由于在第一次测试下只有一个 class,因此将首先执行 class 下的测试。 In my case Test Class A which is responsible to test some functionality as well as setup some data.在我的例子中测试 Class A 负责测试一些功能以及设置一些数据。 Then the next test of your testng.xml will instantiate the other classes in parallel.然后您的 testng.xml 的下一个测试将并行实例化其他类。

Below is how my testng.xml looks下面是我的 testng.xml 的样子


    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

    <suite name="Test_Suite_Name" verbose="1" parallel="classes" thread-count="4">
        <test name="TEST_TESTCLASSA" preserve-order="true">
            <parameter name="environment" value="macos"/>
            <groups>
                <run>
                    <include name="Smoke"/>
                    <exclude name="Sanity"/>
                    <exclude name="Exclude"/>
                </run>
            </groups>
            <classes>
                <class name="testpackage.TestClassA"/>
            </classes>
        </test>
        <test name="TEST_OTHER_TESTS" preserve-order="true">
            <parameter name="environment" value="macos"/>
            <groups>
                <run>
                    <include name="Smoke"/>
                    <exclude name="Sanity"/>
                    <exclude name="Exclude"/>
                </run>
            </groups>
            <classes>
                <class name="testpackage.TestClassB"/>
                <class name ="testpackage.TestClassC"/>
                <class name="testpackage.TestClassD"/>
                <class name="testpackage.TestClassE"/>
                <class name="testpackage.TestClassF"/>
                <class name="testpackage.TestClassG"/>
            </classes>
        </test>
    </suite>

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

相关问题 您的Application类如何知道应用程序何时完成? - How does your Application class know when the app has finished? JUnit 5 在每个 class 中的所有测试都运行后运行一个方法? - JUnit 5 run a method after all tests in every class has run? 如何在两个不同的数据库上运行JUnit测试 - How to run your JUnit tests on two different databases 如何仅使用gradle wrapper命令在TestSuite类中运行测试 - How to only run tests in a TestSuite class with gradle wrapper command 在Eclipse中,如何运行引用一个类的所有junit测试 - in Eclipse, how to run all junit tests that reference one class 如何在任何类中的任何测试之前和之后运行一组代码? - How to run a set of code before and after any tests in any class? 在Maven中完成集成测试后如何运行软件包目标? - How run package goal after finished integration tests in maven? 黄瓜-如何构建测试步骤? - Cucumber - How to struct your tests steps? 的webdriver。 使用参数化类后无法运行测试 - WebDriver. Not able to run tests after using parametrized class Junit 5.5.1 和 5.5.2 突然无法运行测试:“进程完成,退出代码 -1”; 5.6.0-RC1 缺少一个类 - Junit 5.5.1 and 5.5.2 suddenly failing to run tests: "Process finished with exit code -1"; 5.6.0-RC1 is missing a class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM