peetya
peetya

Reputation: 3628

TestNG runs every method and ignores the included methods from testng.xml

I create my testng.xml programatically and I would like to add each method what I want to run. I'm doing it on the following way right now:

XmlClass myClass = new XmlClass("test.login.LoginTest");
List<XmlInclude> includedMethods = new ArrayList<XmlInclude>();

for (int k = 0; k < 10; k++) {

  includedMethods.add(new XmlInclude("golog" + k));
}

myClass.setIncludedMethods(includedMethods);

According to my generated testng.xml file, it seems it works fine:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="11" verbose="11" name="Login Test" parallel="tests">
  <test name="1" group-by-instances="true">
    <classes>
      <class name="test.login.LoginTest">
        <methods>
          <include name="golog1"/>
          <include name="golog2"/>
          <include name="golog3"/>
          <include name="golog4"/>
          <include name="golog5"/>
          <include name="golog6"/>
          <include name="golog7"/>
          <include name="golog8"/>
          <include name="golog9"/>
        </methods>
      </class> <!-- test.login.LoginTest -->
    </classes>
  </test> <!-- 1 -->
</suite> <!-- Login Test -->

The problem is that when my code get executed after generating the xml file, every @Test method get executed (those methods too which has completely different name, like "gssig01") despite the fact that I didn't include them.

My question is that do I have to maybe exclude every methods before including anything, or I did something wrong? :)

Thank you in advance!

Upvotes: 0

Views: 1279

Answers (1)

Chandan Nayak
Chandan Nayak

Reputation: 10927

Try this code, it works

//Create a list which can contain the classes that you want to run including methods.
 List<XmlClass> myClasses = new ArrayList<XmlClass> ();
 XmlClass xmlclass = new XmlClass("stack1.LoginTest");

 List<XmlInclude> includedMethods = new ArrayList<XmlInclude>();

 for (int k = 0; k < 5; k++) {

   includedMethods.add(new XmlInclude("golog" + k));
 }
 xmlclass.setIncludedMethods(includedMethods);

 myClasses.add(xmlclass);

Here is the output where i had 7 methods and only 4 ran

golog1test1
golog2test2
golog3test3
golog4test4

===============================================
StackOverFlow-Answer
Total tests run: 4, Failures: 0, Skips: 0
===============================================

If you want to check the full class:
RunTestNG.java - Test runner class
LoginTest.java - Test class

Upvotes: 1

Related Questions