TestNG - cannot run same test twice in a suite

前端 未结 2 1761
小鲜肉
小鲜肉 2020-12-21 17:56

I have a TestNG test suite that is working fine except for one thing: I cannot run the same test twice. The reason I want to run a test twice is that I am testing web servic

相关标签:
2条回答
  • 2020-12-21 18:06

    Solving the problem of code duplication in TestNG test I could find only this solution:

    Create a rootsuite.xml file like:

    <suite name="Description">
    <suite-files>
        <suite-file path="../../Actions/Test1.xml"/>
        <suite-file path="../../Actions/Test2.xml"/>
        <suite-file path="Actions/Test3.xml"/>
        <suite-file path="../../Common/Close Browser.xml"/>
    </suite-files>
    
    <suite-files>
        <suite-file path="../../Actions/Test1.xml"/>
        <suite-file path="../../Actions/Test2.xml"/>
        <suite-file path="Actions/Test4.xml"/>
        <suite-file path="../../Common/Close Browser.xml"/>
    </suite-files>
    </suite>
    

    Important! You can not include duplicated suite-files inside Test1.xml, Test2.xml, Test3.xml, Test4.xml - all duplicated suites will be ignored.

    Note the version of TestNG must be at least 6.9.9

    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.9.9</version>
      <scope>test</scope>
    </dependency> 
    
    0 讨论(0)
  • 2020-12-21 18:25

    This is actually by design in TestNG. I see two potential options:

    First, look at using a DataProvider with your tests so they can be executed repeatedly with different parameters.

    Or, alter the XML layout so that each method is part of its own tag. In this configuration, the XML will be longer, but you'll be able to invoke a test method as many times as you like as part of a suite.

    EDIT: More details now.

    In response to your comment, here's a sample XML layout that worked for me when I was up against this same problem. Try this:

    <suite name="WebServiceTestSuite">
    <test name="webServiceTest">
        <classes>
            <class name="com.abcco.webservice.DivisionTest">
                <methods>
                    <include name="registrationCreateTest"></include>
                </methods>
            </class>
        </classes>
    </test>
    <test name="webServiceTest">
        <classes>
            <class name="com.abcco.webservice.DivisionTest">
                <methods>
                    <include name="registrationActivateTest"></include>
                </methods>
            </class>
        </classes>
    </test>
    <test name="webServiceTest">
        <classes>
            <class name="com.abcco.webservice.DivisionTest">
                <methods>
                    <include name="appUserLoginTest"></include>
                </methods>
            </class>
        </classes>
    </test>
    <test name="webServiceTest">
        <classes>
            <class name="com.abcco.webservice.DivisionTest">
                <methods>
                    <include name="appUserLogoutTest"></include>
                </methods>
            </class>
        </classes>
    </test>
    <test name="webServiceTest">
        <classes>
            <class name="com.abcco.webservice.DivisionTest">
                <methods>
                    <include name="appUserLoginTest"></include>
                </methods>
            </class>
        </classes>
    </test>
    <test name="webServiceTest">
        <classes>
            <class name="com.abcco.webservice.DivisionTest">
                <methods>
                    <include name="sessionValidateTest"></include>
                </methods>
            </class>
        </classes>
    </test>
    <test name="webServiceTest">
        <classes>
            <class name="com.abcco.webservice.DivisionTest">
                <methods>
                    <include name="sessionExtendTest"></include>
                </methods>
            </class>
        </classes>
    </test>
    </suite>
    

    I removed the verbose="10" parallel="none" from the <test> tags, as the layout that worked for me didn't have this... You may be able to add the verbose="10" to the <suite> tag to get any specific functionality there that you need. Have a go with this and post your results and I'll be happy to help further. Without seeing your runner configuration or knowing too much about the rest of your setup, there may be some other things to consider.

    You'll find that this is a much more verbose XML layout, but it solved this same problem for me at the time. You could also consider re-arranging your code a bit and making an @Test method that calls all the methods on this XML as a single test, allowing you to reduce the length of your XML file.

    0 讨论(0)
提交回复
热议问题