Executing tests in testng

We can execute tests using XML suite file as shown in below example.

Syntax of Suite file

  • suite tag contains one or more test tag. Each test tag can contains one or more tests to be executed.
  • A test tag can contain tags like classes, groups etc. We can also pass the attribute preserver-order which is used to specify if the tests should be executed in given order.
  • In test tag, preserve-order attribute is used to run the tests in the order as given in XML file
  • In test tag, group-by-instances attribute is used to group tests by test class instances

Running tests from specific classes

 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Softpost Suite">
    <test name="softpostTests" preserve-order="true" >

        <classes>
            <class name="org.softpost.AppTest">
                <methods>
                    <include name="test1"/>
                    <exclude name="test2"/>
                </methods>
            </class>
            <class name="org.softpost.TimeoutTests" />
        </classes>

    </test>
</suite>

Running Tests from package

 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Softpost Suite">
    <test name="softpostTests" preserve-order="true" >

        <packages>
            <package name="org.softpost"/>
        </packages>

    </test>
</suite>

Running tests from Specific Groups

Notice that when running the tests from a group, you need to also specify the packages or classes containing tests from those group.
 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Softpost Suite">
    <test name="softpostTests" preserve-order="true" >

        <groups>
    <define name="desktop_browsers">
      <include name="chrome"/>
      <include name="ie"/>
    </define>
  
    <define name="mobile_browsers">
      <include name="android"/>
      <include name="ios"/>
    </define>
  
    <run>
      <include name="desktop_browsers"/>
      <exclude name="mobile_browsers"/>
    </run>
  </groups>
        <packages>
            <package name="org.softpost"/>
        </packages>
    </test>
</suite>

This is how we can execute tests using suite file in TestNG

Web development and Automation testing

solutions delivered!!