【问题标题】:Need a way for tests to run sequentially in TestNG using xmlSuites需要一种使用 xmlSuites 在 TestNG 中按顺序运行测试的方法
【发布时间】:2020-06-03 11:52:56
【问题描述】:

我不通过 testng xml 运行我的测试用例,而是使用

TestListener listener=new TestListener();
    XmlSuite suite=new XmlSuite();
    suite.setName("Test Results");
    suite.setParallel(ParallelMode.NONE);
    suite.setThreadCount(Integer.parseInt(TestProperties.THREAD_COUNT.toString()));
    List<XmlSuite> suits=new ArrayList<XmlSuite>();
    suits.add(suite);


    List<XmlPackage> xpackage=new ArrayList<XmlPackage>();
    xpackage.add(new XmlPackage(TestProperties.TESTNG_PACKAGE.toString()));


    XmlTest test=new XmlTest(suite);
    test.setPackages(xpackage);
    //test.setParallel(ParallelMode.NONE);
    String groups=TestProperties.TESTNG_GROUP.toString();
    System.out.println("groups are:"+groups);
    String groupArray[]=groups.split(",");
    List<String> includedGroups=new ArrayList<String>();
    includedGroups.addAll(Arrays.asList(groupArray));
    test.setIncludedGroups(includedGroups);


    TestNG tng=new TestNG();
    tng.setOutputDirectory("test-output");
    tng.setXmlSuites(suits);
    //tng.addListener((ITestNGListener) listener);
    tng.run();
    System.exit(0)

由于很多因素,我需要按顺序运行测试。我尝试给出parallelMode.none,保留顺序等,但是我的测试用例以一种通常是数字的奇怪方式运行。例如,在 200 之后,我的代码中的测试用例顺序将是 211,212,213 等,然后是 201,202 等。我需要它按该顺序运行。但是现在,在 200 之后,测试用例运行将是 201。我怎样才能使测试用例按照指定的顺序运行。

我也尝试优先考虑测试用例 211,212 等,但这也没有用。我添加了方法拦截器,但我的行号始终为 1。

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().getMethodName();
        String className  = mi.getMethod().getConstructorOrMethod().getDeclaringClass().getCanonicalName();
        ClassPool pool    = ClassPool.getDefault();

        try {
            CtClass cc        = pool.get(className);
            CtMethod ctMethod = cc.getDeclaredMethod(methodName);
            System.out.println(methodName);
            result            = ctMethod.getMethodInfo().getLineNumber(0);
            System.out.println("result:"+result+"and method:"+ctMethod);
        } catch (NotFoundException e) {
            e.printStackTrace();
        }

        return result;
        }

        public int compare(IMethodInstance m1, IMethodInstance m2) {
            System.out.println(getLineNo(m1) - getLineNo(m2));
        return getLineNo(m1) - getLineNo(m2);
        }
    };

    IMethodInstance[] array = methods.toArray(new IMethodInstance[methods.size()]);
    Arrays.sort(array, comparator);
    System.out.println("22222222222222222222222222222222222222222222222222222222");
    System.out.println(Arrays.asList(array));
    return Arrays.asList(array);
    }

【问题讨论】:

    标签: java selenium automation testng


    【解决方案1】:

    testng XML 文件中有一个叫做 preserve-order = true 的东西。您可以使用它来保留执行顺序。

    对于 testng 的编程执行:在套件级别设置保留顺序

        XmlSuite xmlsuite = new XmlSuite();
        xmlsuite.setPreserveOrder(true);
    

    在测试级别设置保留顺序这里的测试是指测试标签而不是@Test注解。

    List<XmlTest> tests = xmlsuite.getTests();
            for (XmlTest test : tests) {
                test.setPreserveOrder(true);
                .....
    }
    

    如果你想在@Test 的执行中有一些顺序,你总是可以使用priority 和dependOnMethods 以及你的@Test 注解。如果您使用下面示例中指定的dependsOnMethods,则method2 将仅在method1 成功运行后启动。如果 method1 由于某种原因失败,method2 将被标记为已跳过。

    示例:

    @Test(priority=1)
    public void method1(){}
    
    @Test(priority=2)
    public void method2(){}
    
    @Test
    public void method1(){}
    
    @Test(dependsOnMethods="method1")
    public void method2(){}
    

    使用 testng XML 文件执行

    <suite name="Test_Suite_Name" verbose="1" preserve-order="true">
        <test name="TEST_TESTCLASSA" preserve-order="true">
            <classes>
                <class name="testpackage.TestClassA"/>
            </classes>
        </test>
        <test name="TEST_OTHER_TESTS" preserve-order="true">
            <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>
    

    重要说明 如果您使用保留顺序标志,则执行将按照它们出现在列表中的顺序(在程序执行的情况下)和它们出现在 testng xml 文件中的顺序进行。

    一个建议 在使用 testng 的程序执行时,请不要在代码中对类进行硬编码。您始终可以解析现有的 testng XML 文件,并在运行时使用程序化 testng 根据您的要求对其进行调整,然后触发测试执行。

    【讨论】:

    • 我不使用 testng XML,而是通过 testng 线程运行。我确实尝试过保留订单,但这也无济于事。我可以优先考虑,但每个套件中有大约 100 多个测试用例,所以维护将是一个问题
    • @aswathy 您能否调试并检查此行是否按照您尝试执行执行的顺序插入包-> xpackage.add(new XmlPackage(TestProperties.TESTNG_PACKAGE.toString() ));。测试方法也按照方法名称的字母顺序执行。请记住这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-09
    相关资源
    最近更新 更多