Added: -testnames (command line) and testnames (ant)
diff --git a/CHANGES.txt b/CHANGES.txt
index 845bf73..835d231 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,5 +1,6 @@
Current:
+Added: -testnames (command line) and testnames (ant)
Added: New ant task tag: propertyset (Todd Wells)
Added: ITestNGListenerFactory
Added: Passing command line properties via the ant task and doc update (Todd Wells)
diff --git a/src/org/testng/TestNG.java b/src/org/testng/TestNG.java
index 36b5a7c..254fdc8 100644
--- a/src/org/testng/TestNG.java
+++ b/src/org/testng/TestNG.java
@@ -341,7 +341,13 @@
try {
Collection<XmlSuite> allSuites = new Parser(suiteXmlPath).parse();
for (XmlSuite s : allSuites) {
- m_suites.add(s);
+ // If test names were specified, only run these test names
+ if (m_testNames != null) {
+ m_suites.add(extractTestNames(s, m_testNames));
+ }
+ else {
+ m_suites.add(s);
+ }
}
}
catch(FileNotFoundException e) {
@@ -428,6 +434,32 @@
}
/**
+ * If the XmlSuite contains at least one test named as testNames, return
+ * an XmlSuite that's made only of these tests, otherwise, return the
+ * original suite.
+ */
+ private static XmlSuite extractTestNames(XmlSuite s, List<String> testNames) {
+ List<XmlTest> tests = Lists.newArrayList();
+ for (XmlTest xt : s.getTests()) {
+ for (String tn : testNames) {
+ if (xt.getName().equals(tn)) {
+ tests.add(xt);
+ }
+ }
+ }
+
+ if (tests.size() == 0) {
+ return s;
+ }
+ else {
+ XmlSuite result = (XmlSuite) s.clone();
+ result.getTests().clear();
+ result.getTests().addAll(tests);
+ return result;
+ }
+ }
+
+ /**
* Define the number of threads in the thread pool.
*/
public void setThreadCount(int threadCount) {
@@ -689,6 +721,9 @@
private Injector m_injector;
+ /** The list of test names to run from the given suite */
+ private List<String> m_testNames;
+
/**
* Sets the level of verbosity. This value will override the value specified
* in the test suites.
@@ -1064,6 +1099,10 @@
setTestClasses(classes);
}
+ List<String> testNames = (List<String>) cmdLineArgs.get(TestNGCommandLineArgs.TEST_NAMES_COMMAND_OPT);
+ if (testNames != null) {
+ setTestNames(testNames);
+ }
List<String> testNgXml = (List<String>) cmdLineArgs.get(TestNGCommandLineArgs.SUITE_DEF_OPT);
if (null != testNgXml) {
setTestSuites(testNgXml);
@@ -1131,6 +1170,10 @@
}
}
+ private void setTestNames(List<String> testNames) {
+ m_testNames = testNames;
+ }
+
private void setSkipFailedInvocationCounts(Boolean skip) {
m_skipFailedInvocationCounts = skip;
}
diff --git a/src/org/testng/TestNGAntTask.java b/src/org/testng/TestNGAntTask.java
index 17794bb..8f6b7f6 100755
--- a/src/org/testng/TestNGAntTask.java
+++ b/src/org/testng/TestNGAntTask.java
@@ -149,6 +149,8 @@
*/
private List<ReporterConfig> reporterConfigs = Lists.newArrayList();
+ private String m_testNames = "";
+
public void setParallel(String parallel) {
m_parallelMode= parallel;
}
@@ -340,6 +342,10 @@
}
}
+ public void setTestNames(String testNames) {
+ m_testNames = testNames;
+ }
+
/**
* Creates a nested src Path like.
*
@@ -612,6 +618,11 @@
argv.add(m_testName);
}
+ if (!"".equals(m_testNames)) {
+ argv.add(TestNGCommandLineArgs.TEST_NAMES_COMMAND_OPT);
+ argv.add(m_testNames);
+ }
+
if (!reporterConfigs.isEmpty()) {
for (ReporterConfig reporterConfig : reporterConfigs) {
argv.add(TestNGCommandLineArgs.REPORTER);
diff --git a/src/org/testng/TestNGCommandLineArgs.java b/src/org/testng/TestNGCommandLineArgs.java
index b2f65a1..981341e 100644
--- a/src/org/testng/TestNGCommandLineArgs.java
+++ b/src/org/testng/TestNGCommandLineArgs.java
@@ -1,6 +1,13 @@
package org.testng;
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
import org.testng.collections.Lists;
import org.testng.collections.Maps;
import org.testng.internal.AnnotationTypeEnum;
@@ -9,13 +16,6 @@
import org.testng.internal.version.VersionInfo;
import org.testng.log4testng.Logger;
-import java.io.BufferedReader;
-import java.io.FileReader;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-
/**
* TestNG/RemoteTestNG command line arguments parser.
*
@@ -66,6 +66,8 @@
public static final String SUITE_NAME_OPT = "-suitename";
/** The list of test classes option. */
public static final String TESTCLASS_COMMAND_OPT = "-testclass";
+ /** List of test names */
+ public static final String TEST_NAMES_COMMAND_OPT = "-testnames";
public static final String TESTJAR_COMMAND_OPT = "-testjar";
public static final String TEST_NAME_OPT = "-testname";
public static final String TESTRUNNER_FACTORY_COMMAND_OPT = "-testrunfactory";
@@ -240,6 +242,26 @@
TestNG.exitWithError("-testclass must be followed by a classname");
}
}
+ else if (TEST_NAMES_COMMAND_OPT.equalsIgnoreCase(argv[i])) {
+ if ((i + 1) < argv.length) {
+ String nextArg = argv[i + 1].trim();
+ if (! nextArg.startsWith("-")) {
+ List<String> l = (List<String>) arguments.get(TEST_NAMES_COMMAND_OPT);
+ if (null == l) {
+ l = Lists.newArrayList();
+ arguments.put(TEST_NAMES_COMMAND_OPT, l);
+ }
+ l.addAll(Arrays.asList(nextArg.split(",")));
+ i++;
+ }
+ else {
+ break;
+ }
+ }
+ else {
+ TestNG.exitWithError(TEST_NAMES_COMMAND_OPT + " must be followed by a one or more test names");
+ }
+ }
else if (TESTJAR_COMMAND_OPT.equalsIgnoreCase(argv[i])) {
if ((i + 1) < argv.length) {
arguments.put(TESTJAR_COMMAND_OPT, argv[i + 1].trim());
@@ -710,6 +732,8 @@
System.out.println("\t\tdefault output directory to : " + TestNG.DEFAULT_OUTPUTDIR);
System.out.println("[" + TESTCLASS_COMMAND_OPT
+ " list of .class files or list of class names]");
+ System.out.println("[" + TEST_NAMES_COMMAND_OPT
+ + " one or more test names that will be found in the XML file]");
System.out.println("[" + SRC_COMMAND_OPT + " a source directory]");
if (VersionInfo.IS_JDK14) {