Exclude Test in testNg

2019-09-20 18:01发布

问题:

I have the following testNg XML file. I would like to run Test1 and not Test2. How can I do it? I tried excluding the methods of Test2 using the "exclude" keyword. But it doesn't seem to work that. I cannot make changes to the actual code. Unfortunately, that is forbidden. All I can do is make changes to this XML and run the tests. Is there any way using which I can ignore Test2 while still manage to run Test1? I am looking for a more elegant way than commenting the code out.

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<parameter name="xxx" value="yyy" />
<parameter name="zzz" value="aaa" />
<parameter name="user" value="1" />

<test name="Test1">
    <parameter name="browser" value="Chrome" />
    <define name = "regression">
            <include name = "spot" />
            <include name = "sanity" />
        </define>
    <groups>
        <run>
            <include name="regression" />
        </run>
    </groups>
        <classes>
            <class name="class2" />
            <method>
                <include name="method1" />
                <include name="method2" />
            </method> 
        </classes>
</test>

<test name="Test2">
    <parameter name="browser" value="Firefox" />
        <define name = "regression">
            <include name = "spot" />
            <include name = "sanity" />
        </define>
    <groups>
        <run>
            <include name="spot" />
            <exclude name="sanity" />
        </run>
    </groups>
        <classes>
            <class name="class2" />
            <method>
                <include name="method1" />
                <include name="method2" />
            </method> 
        </classes>
</test>



</suite>  <!-- Suite -->

回答1:

In test2 replace include to exclude in all fields

by doing this you will exclude that fields or methods from your code



回答2:

If you don't want to run <test name="Test2">, just remove it from the suite.

The idea behind the xml suite file is defining a run. It looks you have many definitions of a run, so you should have many suite files. If you want to share things between suite files, you can use the <suite-file> which is like "import": http://testng.org/testng-1.0.dtd.php

Test1-suite.xml

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
    <parameter name="xxx" value="yyy" />
    <parameter name="zzz" value="aaa" />
    <parameter name="user" value="1" />

    <test name="Test1">
        <parameter name="browser" value="Chrome" />
        <define name = "regression">
            <include name = "spot" />
            <include name = "sanity" />
        </define>
        <groups>
            <run>
                <include name="regression" />
            </run>
        </groups>
        <classes>
            <class name="class2" />
            <method>
                <include name="method1" />
                <include name="method2" />
            </method> 
        </classes>
    </test>
</suite>

Both-suite.xml

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
    <suite-files>
        <suite-file>Test1-suite.xml</suite-file>
    </suite-files>
    <test name="Test2">
        <parameter name="browser" value="Firefox" />
            <define name = "regression">
                <include name = "spot" />
                <include name = "sanity" />
            </define>
        <groups>
            <run>
                <include name="spot" />
                <exclude name="sanity" />
            </run>
        </groups>
        <classes>
            <class name="class2" />
            <method>
                <include name="method1" />
                <include name="method2" />
            </method> 
        </classes>
    </test>
</suite>


标签: testng