3

Using the following XML file, I can run all of the tests in the some.package.login Java package.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Login">
  <test name="Login">
    <packages>
      <package name="some.package.login"/>
    </packages>
  </test>
</suite>

I would like to run TestNG from the command line which does the same as the above XML file but without the XML file. (This question is asking how to run with the XML file.) I know about the -suitename parameter. I just don't know how to limit the execution to a specific package.

I tried -testclass some.package.login.* but TestNG treats this as a name of a class and doesn't handle wild cards.

I could use -groups and then use @Test(groups = "Login") on all of the tests but I would rather not have to do that.

I am looking for a solution which is easier than creating an XML file for each subset of tests I want to run. Writing a class for each subset isn't any easier than writing an XML file.

In case this is not possible to do, I have filed a TestNG enhancement request.

Community
  • 1
  • 1
Nathan
  • 8,093
  • 8
  • 50
  • 76
  • The possible duplicate is not a duplicate since my question is asking how to run *without* a XML file and the possible duplicate question is asking how to run *with* a XML file. – Nathan Oct 31 '16 at 16:41

2 Answers2

1

You'll be able to filter tests with a method interceptor.

public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
  List<IMethodInstance> result = new ArrayList<IMethodInstance>();
  for (IMethodInstance m : methods) {
    String expectedPackage = System.getProperty("package");
    Package p = m.getInstance().getClass().getPackage();
    if (p matches expectedPackage) {
      result.add(m);
    }
  }
  return result;
}

I let you complete the logic of match. Then, just add an environment variable in the command line like:

java -Dpackage="myPackage" -jar ....
juherr
  • 5,640
  • 1
  • 21
  • 63
0

It could be done via factory (using -testrunfactory in CLI), but it will require some additional refactoring. Or just specify xml in package and use -xmlpathinjar. Also you can create xml dynamically during project build phase.

RocketRaccoon
  • 2,559
  • 1
  • 21
  • 30
  • I already have the XML file and I simply put that on the command line. I was hoping to skip the XML file altogether via command line options. – Nathan Nov 01 '16 at 15:43
  • So it could be solved by running TestNG programatically – RocketRaccoon Nov 02 '16 at 03:56
  • I am looking for a solution which is easier than an XML file. The XML file is easier than creating a class for each test I want to run in isolation. – Nathan Nov 02 '16 at 14:39
  • We are just generating xml for each package automatically using some predefined rules (exclude some groups, or by annotation value). It helps sometimes to investigate issues if something going wrong during execution. We have over 30 test pacakges and 15k tests at all. The easiest way could be to use test discovery feature of TestNG but it less manageable than xml. – RocketRaccoon Nov 02 '16 at 19:02