Can TestNG run multiple suites?

2020-02-08 21:05发布

问题:

I am testing a web-ui using Selenium and TestNG. I have a test suite with many test classes in it. I have a @BeforeSuite method which also has a @Parameters annotation, this method receives as a parameter the browser in which the Selenium test will be run, executing the lines:

selenium = new DefaultSelenium("localhost", 4444, browser, "http://localhost:8099");
selenium.start();

The XML I'm using to run the test suite is:

<suite name="suite">
<parameter name = "browser" value = "*firefox"/>
 <test name="allTests">
  <classes>
   <class name="test.webui.MemcachedDeploymentTest" />
  </classes>
 </test> 
</suite>

This works fine and the test runs in Firefox. my problem is that i would like to somehow run this suite again, immediately after the first run finishes, but this time with Chrome as the browser. i now have 2 XML suites, one with Chrome and one with Firefox. Is there any way to run these test suites one after the other automatically? maybe using a third XML?

回答1:

You can runt testNG suites like this:

<suite name="allSuites">
  <suite-files>
    <suite-file path="suite1.xml" />
    <suite-file path="suite2.xml" />
    ...
  </suite-files>
</suite>

You can also run those suites in parallel with an ant task. If you want I ll provide example code for ant.



回答2:

Put your parameter tag inside the test tag and create another test tag:

<suite name="suite">
    <test name="Firefox tests">
        <parameter name="browser" value="*firefox" />
        <classes>
            <class name="test.webui.MemcachedDeploymentTest" />
        </classes>
    </test>
    <test name="Chrome tests">
        <parameter name="browser" value="*chrome" />
        <classes>
            <class name="test.webui.MemcachedDeploymentTest" />
        </classes>
    </test>
</suite>

Another option that would be less verbose would be to use @Factory.



回答3:

To Run Multiple suites using TestNG XML the correct code goes below, Where I have prepared Three suites suiteA.xml, suiteB.xml, suiteC.xml and have consolidated them in testng.xml. You can copy paste the below code and change the packagename.classname in the class tag and run it would work...

suiteA.xml

 <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    <suite name="SuiteA"  > 
    <!-- suite name="Suite Name" --> 
                 <test name="TestA1" allow-return-values="true">
                         <classes>
                           <!-- packagename.Testcase class name  -->
                                 <class name ="com.qtpselenium.suiteA.TestCaseA1" />
                         </classes>
                 </test>
                 <test name="TestA2" allow-return-values="true">
                         <classes>
                           <!-- packagename.Testcase class name  -->
                                 <class name ="com.qtpselenium.suiteA.TestCaseA1" />
                         </classes>
                 </test>
    </suite>

suiteB.xml

   <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    <suite name="SuiteB"  > 
    <!-- suite name="Suite Name" --> 
                 <test name="TestB1" allow-return-values="true">
                         <classes>
                           <!-- packagename.Testcase class name  -->
                                 <class name ="com.qtpselenium.suiteB.TestCaseB1" />
                         </classes>
                 </test>
                 <test name="TestB2" allow-return-values="true">
                         <classes>
                           <!-- packagename.Testcase class name  -->
                                 <class name ="com.qtpselenium.suiteB.TestCaseB2" />
                         </classes>
                 </test>
</suite>

suiteC.xml

  <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
        <suite name="SuiteC"  > 
        <!-- suite name="Suite Name" --> 
                     <test name="TestC1" allow-return-values="true">
                             <classes>
                               <!-- packagename.Testcase class name  -->
                                     <class name ="com.qtpselenium.suiteC.TestCaseC1" />
                             </classes>
                     </test>
                     <test name="TestC2" allow-return-values="true">
                             <classes>
                               <!-- packagename.Testcase class name  -->
                                     <class name ="com.qtpselenium.suiteC.TestCaseC2" />
                             </classes>
                     </test>
        </suite>

testng.xml

 <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    <suite name="TestNG Dadadriver suite"  > 
    <!-- suite name="Suite Name" --> 
            <suite-files>
                   <suite-file path="./suiteA.xml" />
                   <suite-file path="./suiteB.xml" />
                   <suite-file path="./suiteC.xml" />
            </suite-files>
    </suite>


回答4:

via Maven:

<plugin>
   <artifactId>maven-surefire-plugin</artifactId>
   <configuration>
      <suiteXmlFiles>
         <suiteXmlFile>src/test/resources/unit-testng.xml</suiteXmlFile>
         <suiteXmlFile>src/test/resources/api-testng.xml</suiteXmlFile>
      </suiteXmlFiles>
   </configuration>
</plugin>


回答5:

<suite name="allSuites">
  <suite-files>
    <suite-file path="suite1.xml" />
    <suite-file path="suite2.xml" />
  </suite-files>
</suite>

Works fine !! It will execute the current suit first and then it will execute the included list of suites one by one.