簡體   English   中英

需要一種使用 xmlSuites 在 TestNG 中按順序運行測試的方法

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

我不通過 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);
    }

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 成功運行后啟動。 如果由於某種原因方法 1 失敗,方法 2 將被標記為已跳過。

例子:

@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 對其進行調整,然后觸發測試執行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM