gradle test task execution order of tests

2019-03-06 13:41发布

问题:

When I run my testng tests using command line and with tests defined in a testng.xml file, they run in the order they are specified in the testng.xml

<suite name="WorkDepot Tests">
        <test name="Submit work-result test">
          <classes>
                <class name="workdepot.test.SubmitWorkTest"/>
                <class name="workdepot.test.WorkResultTest"/>
                <class name="workdepot.test.SubmitWorkTest"/>
                <class name="workdepot.test.WorkErrorTest"/>
                <class name="workdepot.test.SubmitWorkTest"/>
                <class name="workdepot.test.MultipleHasTest"/>
                <class name="workdepot.test.WorkResultTest"/>
                <class name="workdepot.test.CallbackTest"/>
                <class name="workdepot.test.NegCallBackTest"/>
                <class name="workdepot.test.NegSubmitWork"/>
                <class name="workdepot.test.NegGetWork"/>
                <class name="workdepot.test.NegHasWork"/>
                <class name="workdepot.test.NegSubmitResult"/>
                <class name="workdepot.test.NegSubmitError"/>
                <class name="workdepot.test.NegReadResult"/>
                <class name="workdepot.test.NegMultipleHas"/>
          </classes>
        </test>
</suite>

Now I want to run those using the gradle Test task type and I use include pattern for the same. But the order of execution is now changed (arbitrary).

task integTest(type: Test){
    useTestNG()
    testClassDir = file("build/classes/integTest")

    include "workdepot/test/SubmitWorkTest*"
    include "workdepot/test/WorkResultTest*"
    include "workdepot/test/SubmitWorkTest*"
    include "workdepot/test/WorkErrorTest*"
    include "workdepot/test/SubmitWorkTest*"
    include "workdepot/test/MultipleHasTest*"
    include "workdepot/test/WorkResultTest*"
    include "workdepot/test/CallbackTest*"
    include "workdepot/test/NegCallBackTest*"
    include "workdepot/test/NegSubmitWork*"
    include "workdepot/test/NegGetWork*"
    include "workdepot/test/NegHasWork*"
    include "workdepot/test/NegSubmitResult*"
    include "workdepot/test/NegSubmitError*"
    include "workdepot/test/NegReadResult*"
    include "workdepot/test/NegMultipleHas*"

}

How can I ensure the tests are run in the order as in the testng.xml without changing the test code (i.e., adding dependencies between tests)

回答1:

It cannot be controlled from gradle level both for JUnit and TestNG. As you mentioned test sources can be modified. For JUnit a test suite can be defined to gain control over test execution order. If TestNG has it's equivalent of such test suite it may be the way to go.



回答2:

By default, TestNG will run your tests in the order they are found in the XML file. Otherwise gradle test will not guarantee any order,

you can intercept the methods before they run but that is of no use in this case



标签: gradle testng