Order of execution of tests in TestNG

前端 未结 17 2520
情书的邮戳
情书的邮戳 2020-11-27 12:46

How to customize the order of execution of tests in TestNG?

For example:

public class Test1 {
  @Test
  public void test1() {
      System.out.printl         


        
相关标签:
17条回答
  • 2020-11-27 13:38
    @Test(dependsOnMethods="someBloodyMethod")
    
    0 讨论(0)
  • 2020-11-27 13:38

    If I understand your question correctly in that you want to run tests in a specified order, TestNG IMethodInterceptor can be used. Take a look at http://beust.com/weblog2/archives/000479.html on how to leverage them.

    If you want run some preinitialization, take a look at IHookable http://testng.org/javadoc/org/testng/IHookable.html and associated thread http://groups.google.com/group/testng-users/browse_thread/thread/42596505990e8484/3923db2f127a9a9c?lnk=gst&q=IHookable#3923db2f127a9a9c

    0 讨论(0)
  • 2020-11-27 13:39

    Piggy backing off of user1927494's answer, In case you want to run a single test before all others, you can do this:

    @Test()
    public void testOrderDoesntMatter_1() {
    }
    
    @Test(priority=-1)
    public void testToRunFirst() {
    }
    
    @Test()
    public void testOrderDoesntMatter_2() {
    }
    
    0 讨论(0)
  • 2020-11-27 13:41

    This will work.

    @Test(priority=1)
    public void Test1() {
    
    }
    
    @Test(priority=2)
    public void Test2() {
    
    }
    
    @Test(priority=3)
    public void Test3() {
    
    }
    

    priority encourages execution order but does not guarantee the previous priority level has completed. test3 could start before test2 completes. If a guarantee is needed, then declare a dependency.

    Unlike the solutions which declare dependencies, tests which use priority will execute even if one test fails. This problem with dependencies can be worked around with @Test(...alwaysRun = true...) according to documentation.

    0 讨论(0)
  • 2020-11-27 13:42

    I've faced the same issue, the possible reason is due to parallel execution of testng and the solution is to add Priority option or simply update preserve-order="true" in your testng.xml.

    <test name="Firefox Test" preserve-order="true">
    
    0 讨论(0)
提交回复
热议问题