mkatliarou
mkatliarou

Reputation: 40

How to run tests from xml file in a specific order

I want to run tests in a specific order from xml file. First, I need to test login feature, then others. I have TestRunner class and testng.xml file. But when I run tests through TestRunner they run randomly, even if tests have dependsOnGroups attribute. Here is my code:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" parallel="tests" thread-count="2">
<listeners>
    <listener class-name="com.epam.tat.framework.listeners.SuiteListener"/>
    <listener class-name="com.epam.tat.framework.listeners.TestListener"/>
</listeners>
<test name="test1">
    <classes>
        <class name="com.epam.tat.framework.mail.test.MailLoginTest"/>
        <class name="com.epam.tat.framework.mail.test.MailRuTest" />
    </classes>
</test>
<test name="test2">
    <classes>
        <class name="com.epam.tat.framework.cloud.test.CloudLoginTest"/>
        <class name="com.epam.tat.framework.cloud.test.CloudTest"/>
    </classes>
  </test>
</suite>

Upvotes: 0

Views: 613

Answers (2)

Kovacic
Kovacic

Reputation: 1481

As @cruisepandet said, but You can introduce groups, so multiple tests can be settled under same group like example bellow:

import org.testng.Assert;
import org.testng.annotations.Test;

public class GroupTestExample {
   String message = ".com";
   MessageUtil messageUtil = new MessageUtil(message);

   @Test(groups = { "functest", "checkintest" })
   public void testPrintMessage() {
      System.out.println("Inside testPrintMessage()");
      message = ".com";
      Assert.assertEquals(message, messageUtil.printMessage());
   }

   @Test(groups = { "checkintest" })
   public void testSalutationMessage() {
      System.out.println("Inside testSalutationMessage()");
      message = "test" + ".com";
      Assert.assertEquals(message, messageUtil.salutationMessage());
   }

   @Test(groups = { "functest" })
   public void testingExitMessage() {
      System.out.println("Inside testExitMessage()");
      message = "www." + "test "+".com";
      Assert.assertEquals(message, messageUtil.exitMessage());
   }  
}

and then xml would look something like this:

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Suite1">
   <test name = "test1">
      <groups>
         <run>
            <include name = "functest"/>
         </run>
      </groups>

      <classes>
         <class name = "GroupTestExample" />
      </classes>
   </test>


   <test name = "test2">
      <groups>
         <run>
            <include name = "checkintest"/>
         </run>
      </groups>

      <classes>
         <class name = "GroupTestExample" />
      </classes>
   </test>

</suite>

and you can play with include/exclude groups and add one or multiple classes.

If You would like even more finer granulation, order in test-case running, add priority for each test like this:

  @Test( priority = 4 )
    public void testB1() {
        System.out.println("testB1");
    }

    @Test( priority = 5 )
    public void testB2() {
        System.out.println("testB2");
    }

    @Test( priority = 6 )
    public void testB3() {
        System.out.println("testB3");
    }

So this is just parts of sample code that needs to be tweaked for Your needs. But basically this are the tools to do it.

Upvotes: 1

cruisepandey
cruisepandey

Reputation: 29362

See what TestNG has to say about invoking in a certain order.

Sometimes, you need your test methods to be invoked in a certain order.See the example below :

To make sure a certain number of test methods have completed and succeeded before running more test methods.

@Test
public void serverStartedOk() {}

@Test(dependsOnMethods = { "serverStartedOk" })
public void method1() {}  

In this example, method1() is declared as depending on method serverStartedOk(), which guarantees that serverStartedOk() will always be invoked first.

For more reference : TestNG_dependencies

Upvotes: 1

Related Questions