简体   繁体   English

需要一种使用 xmlSuites 在 TestNG 中按顺序运行测试的方法

[英]Need a way for tests to run sequentially in TestNG using xmlSuites

I do not run my testcases through testng xml but use我不通过 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)

I need my tests to run sequentially because of a lot of factors.由于很多因素,我需要按顺序运行测试。 I tried giving parallelMode.none, preserve order etc, but my testcases are being run in a weird way usually numerical.我尝试给出parallelMode.none,保留顺序等,但是我的测试用例以一种通常是数字的奇怪方式运行。 For eg after 200, my testcase order in code would be 211,212,213 etc and after that 201,202 etc. I need it run in that order.例如,在 200 之后,我的代码中的测试用例顺序将是 211,212,213 等,然后是 201,202 等。我需要它按该顺序运行。 But right now, after 200, the testcase run would be 201. How can i make the testcase run with the order in which they are specified.但是现在,在 200 之后,测试用例运行将是 201。我怎样才能使测试用例按照指定的顺序运行。

I also tried giving priority to the testcases 211,212 etc, but that too didnt work.我还尝试优先考虑测试用例 211,212 等,但这也不起作用。 I added method inteceptor but my line number always give 1.我添加了方法拦截器,但我的行号始终为 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);
    }

There is something called preserve-order = true in testng XML file. testng XML 文件中有一个叫做 preserve-order = true 的东西。 You could use that one to preserve the execution order.您可以使用它来保留执行顺序。

For programmatic execution of testng: Setting preserve order at suite level对于 testng 的编程执行:在套件级别设置保留顺序

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

Setting preserve order at test level The test here means the test tag not the @Test annotation.在测试级别设置保留顺序这里的测试是指测试标签而不是@Test 注释。

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

If you want some order in the execution of @Test, you could always use priority and dependsOnMethods along with your @Test annotation.如果您希望在执行@Test 时有某种顺序,您可以始终使用priority 和dependOnMethods 以及您的@Test 注释。 If you use dependsOnMethods as specified in the below example, then the method2 will only start after the method1 has successfully run.如果您使用下面示例中指定的dependsOnMethods,则method2 将仅在method1 成功运行后启动。 If method1 failed due to some reason method2 will be marked as skipped.如果由于某种原因方法 1 失败,方法 2 将被标记为已跳过。

Example:例子:

@Test(priority=1)
public void method1(){}

@Test(priority=2)
public void method2(){}

@Test
public void method1(){}

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

For execution using testng XML file使用 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>

An important note If you use the preserve-order flag, execution will happen in the order as they appear in the List in case of programmatic execution and in the order as they appear in the testng xml file.重要说明如果您使用保留顺序标志,在程序执行的情况下,将按照它们在列表中出现的顺序以及它们在 testng xml 文件中出现的顺序执行。

One suggestion While using programmatic execution of testng, please do not hardcode classes in your code.一个建议在使用 testng 的编程执行时,请不要在代码中对类进行硬编码。 You could always parse an existing testng XML file and tweak it as per your requirement during the runtime using programmatic testng then trigger the test execution.您始终可以解析现有的 testng XML 文件,并在运行时根据您的要求使用编程 testng 对其进行调整,然后触发测试执行。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM