Order of execution of tests in TestNG

前端 未结 17 2535
情书的邮戳
情书的邮戳 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:19

    The ordering of methods in the class file is unpredictable, so you need to either use dependencies or include your methods explicitly in XML.

    By default, TestNG will run your tests in the order they are found in the XML file. If you want the classes and methods listed in this file to be run in an unpredictible order, set the preserve-order attribute to false

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

    By using priority paramenter for @Test we can control the order of test execution.

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

    Use this:

    public class TestNG
    {
            @BeforeTest
            public void setUp() 
            {
                       /*--Initialize broowsers--*/
    
            }
    
            @Test(priority=0)
            public void Login() 
            {
    
            }
    
            @Test(priority=2)
            public void Logout() 
            {
    
            }
    
            @AfterTest
            public void tearDown() 
            {
                    //--Close driver--//
    
            }
    
    }
    

    Usually TestNG provides number of annotations, We can use @BeforeSuite, @BeforeTest, @BeforeClass for initializing browsers/setup.

    We can assign priority if you have written number of test cases in your script and want to execute as per assigned priority then use: @Test(priority=0) starting from 0,1,2,3....

    Meanwhile we can group number of test cases and execute it by grouping. for that we will use @Test(Groups='Regression')

    At the end like closing the browsers we can use @AfterTest, @AfterSuite, @AfterClass annotations.

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

    If you don't want to use the @Test(priority = ) option in TestNG, you can make use of the javaassist library and TestNG's IMethodInterceptor to prioritize the tests according to the order by which the test methods are defined in the test class. This is based on the solution provided here.

    Add this listener to your test class:

    package cs.jacob.listeners;
    
    import java.util.Arrays;
    import java.util.Comparator;
    import java.util.List;
    
    import javassist.ClassPool;
    import javassist.CtClass;
    import javassist.CtMethod;
    import javassist.NotFoundException;
    
    import org.testng.IMethodInstance;
    import org.testng.IMethodInterceptor;
    import org.testng.ITestContext;
    
    public class PriorityInterceptor implements IMethodInterceptor {
        public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
    
        Comparator<IMethodInstance> comparator = new Comparator<IMethodInstance>() {
            private int getLineNo(IMethodInstance mi) {
            int result = 0;
    
            String methodName = mi.getMethod().getConstructorOrMethod().getMethod().getName();
            String className  = mi.getMethod().getConstructorOrMethod().getDeclaringClass().getCanonicalName();
            ClassPool pool    = ClassPool.getDefault();
    
            try {
                CtClass cc        = pool.get(className);
                CtMethod ctMethod = cc.getDeclaredMethod(methodName);
                result            = ctMethod.getMethodInfo().getLineNumber(0);
            } catch (NotFoundException e) {
                e.printStackTrace();
            }
    
            return result;
            }
    
            public int compare(IMethodInstance m1, IMethodInstance m2) {
            return getLineNo(m1) - getLineNo(m2);
            }
        };
    
        IMethodInstance[] array = methods.toArray(new IMethodInstance[methods.size()]);
        Arrays.sort(array, comparator);
        return Arrays.asList(array);
        }
    }
    

    This basically finds out the line numbers of the methods and sorts them by ascending order of their line number, i.e. the order by which they are defined in the class.

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

    In TestNG, you use dependsOnMethods and/or dependsOnGroups:

    @Test(groups = "a")
    public void f1() {}
    
    @Test(groups = "a")
    public void f2() {}
    
    @Test(dependsOnGroups = "a")
    public void g() {}
    

    In this case, g() will only run after f1() and f2() have completed and succeeded.

    You will find a lot of examples in the documentation: http://testng.org/doc/documentation-main.html#test-groups

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

    To address specific scenario in question:

    @Test
    public void Test1() {
    
    }
    
    @Test (dependsOnMethods={"Test1"})
    public void Test2() {
    
    }
    
    @Test (dependsOnMethods={"Test2"})
    public void Test3() {
    
    }
    
    0 讨论(0)
提交回复
热议问题