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.

1
  • 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. Commented Oct 31, 2016 at 16:41

2 Answers 2

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 .... 
Sign up to request clarification or add additional context in comments.

Comments

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.

4 Comments

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.
So it could be solved by running TestNG programatically
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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.