如何在一个类完成其测试后,仅在类级别并行运行你的测试。

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

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

问题

我陷入了一个相当独特的问题,我确信你们中有些人在某个时候可能也面临过类似的问题。

所以我在我的TestNg测试套件中总共有10个测试类,我希望它们能并行运行。这很容易,但问题是我的一个类,假设是Test A类,不仅设置了测试数据,还测试了应用程序的某些功能,因此我不能将其放在BeforeSuite或BeforeTest注释下。通过执行此类测试而创建的测试数据对于其他测试类的运行很重要,因此我真的不知道如何实现类级别的并行性,以便在Test A类下的测试完成执行后才启动并行运行。

摘要

Test A类

public class TestsA {

   @BeforeClass
   public setupTestClass(){
   -----一些代码--------
  }
  @Test
  public test1(){
 ------一些代码---
}

我希望这个TestA类首先运行,然后并行实例化其他测试类。关于这方面的任何帮助将不胜感激。

谢谢

英文:

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

Summary

Class Test 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.
Any help regarding this would be really appreciated.

Thanks

答案1

得分: 0

默认情况下,所有的测试方法都将具有优先级 '0'。因此,如果您想要在所有其他测试之前执行特定的测试方法,您可以为该特定测试设置优先级为 -1。优先级较低的测试方法将在 TestNG 中首先执行。因此,具有优先级 -1 的测试将在具有优先级 '0' 的所有其他测试之前执行。

示例 为单个测试方法设置优先级

public class ClassA {

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

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

示例 2: 为整个类设置优先级

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

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

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

两个示例的输出:

Test1

Test2

英文:

By default all the Test methods will have a priority '0' value. 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. Lesser the priority first will be its execution in testng. So test with priority as -1 precedes all the other tests with priority '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

@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:

Test1

Test2

答案2

得分: 0

我已找到上述问题的解决方案。我所需要做的就是在我的testng.xml套件上配置两个不同的测试。第一个测试将只包含测试类A,而另一个测试将包含其他测试类,这些类在类级别进行并行处理。由于在第一个测试中只有一个类,该类下的测试将首先被执行。在我的情况下,测试类A负责测试某些功能并设置一些数据。然后testng.xml的下一个测试将并行实例化其他类。

以下是我的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>
英文:

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. 1st test will have only Test class A and the other test will have other test classes having parallelism at class level. Since under 1st test there's only one class, the tests under that class will be executed first. In my case Test Class A which is responsible to test some functionality as well as setup some data. Then the next test of your testng.xml will instantiate the other classes in parallel.

Below is how my testng.xml looks


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

</details>



huangapple
  • 本文由 发表于 2020年5月30日 17:17:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/62100345.html
匿名

发表评论

匿名网友

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

确定