简体   繁体   中英

Is there any way we can replace multiple test tags in testng.xml with something else

I have a testng.xml which contains multiple test tags and each test tag has two classes. for now there are 10-12 test cases so there are 10-12 test tags but in future this number might increase to 50+ then in that case the testng.xml will be very heavy. Is there any way we can decrease the line of code.

The second class VerifySuite has @Test annotation and cannot be changed as it is coming from a jar.

Below is the xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <listeners>
        <listener class-name="com.api.reporting.ExtentReporterNG" />
    </listeners>
    <test name="GET TC001_TC002">
        <classes>
            <class name="com.test.scenarios.TC001_TC002" />
            <class name="com.test.api.verification.VerifySuite" />
        </classes>
    </test>
    <test name="GET TC003_TC004">
        <classes>
            <class name="com.test.scenarios.TC003_TC004" />
            <class name="com.test.api.verification.VerifySuite" />
        </classes>
    </test>
    <test name="GET TC005_TC006">
        <classes>
            <class name="com.test.scenarios.TC005_T006" />
            <class name="com.test.api.verification.VerifySuite" />
        </classes>
    </test>
    <test name="GET TC007">
        <classes>
            <class name="com.test.scenarios.TC007" />
            <class name="com.test.api.verification.VerifySuite" />
        </classes>
    </test>
    <test name="POST TC001_TC002">
        <classes>
            <class name="com.test.scenarios.TC001_TC002" />
            <class name="com.test.api.verification.VerifySuite" />
        </classes>
    </test>
</suite> <!-- Suite -->

Yes you can do this pretty easily using TestNG. Here's how

  1. Make sure you are using the latest released version of TestNG viz., 6.11
  2. Create an implementation of the TestNG listener viz., org.testng.IAlterSuiteListener wherein within the org.testng.IAlterSuiteListener#alter() method, you basically start creating the XmlTest objects via code and then inject them into the XmlSuite object.

You can even get a bit fancy on this wherein you can have your alter() method feed off a data source such as an excel spreadsheet or a csv or a json file which contains the name of the <test> tag and the set of test classes that are to be included in the ` tag and have this listener weave the entire suite xml file programatically.

In your testng.xml suite xml file, you will only specify the <listeners> tag and add a reference to the IAlterSuiteListener implementation which you built.

That should help you do what you are after.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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