简体   繁体   中英

Is there a way to run 1 test-group multiple times with some selected methods using TestNG XML file?

I have a group called 'base application group' which consist of 3 test methods:

Test a(groups = "base application group"), 
Test b(groups = "base application group") AND 
Test c(groups = "base application group").

Now, say that I only want to run Test a() and want to exclude rest of the methods from the group. I know how to do this in XML file. Now the real question is How can I run this group with every class in the XML File, provided I have 10 classes in XML file and this group should be executed 10 times?

So far, I have tried doing this with include and exclude tags at class level but I dont want to do it 10 times in my XML file. I have also tried tag and Meta Group but that doesn't give me the desired output.

Here is my xml code atm:

<suite name ="Footer Suite" >
<test name ="Footer Tests" verbose ="2" >
    <classes>
        <class name ="it.org.techtime.jira.easysso.seleniumtests.footertests.MainAdminScreenTests">
            <methods>
                <exclude name ="Test b"/>
                <exclude name ="Test c"/>
            </methods>
        </class>
        <class name ="it.org.techtime.jira.easysso.seleniumtests.footertests.MainAdminScreenTests">
            <methods>
                <exclude name ="Test b"/>
                <exclude name ="Test c"/>
            </methods>
        </class>
    </classes>
</test>

I expect 'base application group' to run 10 times and also want only Test a() to run from the group. Not to forget that I want to achieve this from XML file.

You cannot do this out of the box with TestNG. There's no mechanism that basically iterates a bunch of tests that belong to a group, n times.

The only way to do this is to do this is to manually duplicate the <test> tags, which as you pointed out is a manual process and needs the suite xml to be changed and checked-in every time.

All said and done, TestNG still lets you do this by doing the following.

  1. Make sure you are depending on TestNG 7.0.0-beta7 (latest as of today)
  2. Build an implementation of org.testng.IAlterSuiteListener using which you can programmatically alter your suite file and add/remove/update anything in it.

Here's an example of a listener that does what you are asking for.

import java.util.List;
import org.testng.IAlterSuiteListener;
import org.testng.ITestContext;
import org.testng.TestListenerAdapter;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlGroups;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

public class MultiRunner extends TestListenerAdapter implements IAlterSuiteListener {

  @Override
  public void alter(List<XmlSuite> suites) {
    XmlSuite xmlsuite = suites.get(0);
    XmlTest xmltest = xmlsuite.getTests().get(0);
    XmlGroups xmlgroups = xmltest.getXmlGroups();
    List<XmlClass> xmlclasses = xmltest.getClasses();
    int iteration = Integer.parseInt(System.getProperty("iterations", "2"));
    String name = xmltest.getName();
    for (int i=1; i<= iteration; i++) {
      XmlTest test = new XmlTest(xmlsuite);
      test.setName(name + "_" + i);
      test.setGroups(xmlgroups);
      test.setXmlClasses(xmlclasses);
    }
  }

  @Override
  public void onStart(ITestContext testContext) {
    System.err.println("Commencing execution of [" + testContext.getName() + "]");
  }
}

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