3
3
import java .util .List ;
4
4
import org .testng .TestNGException ;
5
5
import org .testng .collections .Lists ;
6
+ import org .testng .log4testng .Logger ;
7
+ import org .testng .util .Strings ;
6
8
import org .testng .xml .XmlSuite ;
7
9
import org .testng .xml .XmlTest ;
8
10
9
- /** The class to work with "-testnames" */
11
+ /**
12
+ * The class to work with "-testnames", "-ignoreMissedTestNames", and VM argument
13
+ * "-Dtestng.ignore.missed.testnames". If both "-ignoreMissedTestNames" and VM argument
14
+ * "-Dtestng.ignore.missed.testnames" are set, then either of them has "true" value will enable the
15
+ * feature to ingore partially missed test names and run those existing test names.
16
+ */
10
17
public final class TestNamesMatcher {
11
18
19
+ private static final Logger LOGGER = Logger .getLogger (TestNamesMatcher .class );
20
+
12
21
private final List <XmlSuite > cloneSuites = Lists .newArrayList ();
13
22
private final List <String > matchedTestNames = Lists .newArrayList ();
14
23
private final List <XmlTest > matchedTests = Lists .newArrayList ();
15
24
private final List <String > testNames ;
25
+ private final boolean ignoreMissedTestNames ;
16
26
17
27
public TestNamesMatcher (XmlSuite xmlSuite , List <String > testNames ) {
28
+ this (xmlSuite , testNames , false );
29
+ }
30
+
31
+ public TestNamesMatcher (
32
+ XmlSuite xmlSuite , List <String > testNames , boolean ignoreMissedTestNames ) {
18
33
this .testNames = testNames ;
34
+ this .ignoreMissedTestNames = ignoreMissedTestNames ;
19
35
cloneIfContainsTestsWithNamesMatchingAny (xmlSuite , this .testNames );
20
36
}
21
37
@@ -26,7 +42,7 @@ public TestNamesMatcher(XmlSuite xmlSuite, List<String> testNames) {
26
42
* @param testNames The list of testnames to iterate through
27
43
*/
28
44
private void cloneIfContainsTestsWithNamesMatchingAny (XmlSuite xmlSuite , List <String > testNames ) {
29
- if (testNames == null || testNames . isEmpty ( )) {
45
+ if (Strings . isBlankStringList ( testNames )) {
30
46
throw new TestNGException ("Please provide a valid list of names to check." );
31
47
}
32
48
@@ -43,13 +59,38 @@ public List<XmlSuite> getSuitesMatchingTestNames() {
43
59
return cloneSuites ;
44
60
}
45
61
46
- public void validateMissMatchedTestNames () {
47
- List <String > tmpTestNames = Lists .newArrayList ();
48
- tmpTestNames .addAll (testNames );
49
- tmpTestNames .removeIf (matchedTestNames ::contains );
50
- if (!tmpTestNames .isEmpty ()) {
51
- throw new TestNGException ("The test(s) <" + tmpTestNames + "> cannot be found in suite." );
62
+ /**
63
+ * Do validation for testNames and notify users if any testNames are missed in suite. This method
64
+ * is also used to decide how to run test suite when test names are given. In legacy logic, if
65
+ * test names are given and exist in suite, then run them; if any of them do not exist in suite,
66
+ * then throw exception and exit. After ignoreMissedTestNames is introduced, if
67
+ * ignoreMissedTestNames is enabled, then any of the given test names exist in suite will be run,
68
+ * and print warning message to tell those test names do not exist in suite.
69
+ *
70
+ * @return boolean if ignoreMissedTestNames disabled, then return true if no missed test names in
71
+ * suite, otherwise throw TestNGException; if ignoreMissedTestNames enabled, then return true
72
+ * if any test names exist in suite, otehrwise (all given test names are missed) throw
73
+ * TestNGException.
74
+ */
75
+ public boolean validateMissMatchedTestNames () {
76
+ final List <String > missedTestNames = getMissedTestNames ();
77
+ if (!missedTestNames .isEmpty ()) {
78
+ final String errMsg = "The test(s) <" + missedTestNames + "> cannot be found in suite." ;
79
+ if (ignoreMissedTestNames && !matchedTestNames .isEmpty ()) {
80
+ LOGGER .warn (errMsg );
81
+ return true ;
82
+ } else {
83
+ throw new TestNGException (errMsg );
84
+ }
52
85
}
86
+ return missedTestNames .isEmpty () && !matchedTestNames .isEmpty ();
87
+ }
88
+
89
+ public List <String > getMissedTestNames () {
90
+ List <String > missedTestNames = Lists .newArrayList ();
91
+ missedTestNames .addAll (testNames );
92
+ missedTestNames .removeIf (matchedTestNames ::contains );
93
+ return missedTestNames ;
53
94
}
54
95
55
96
public List <XmlTest > getMatchedTests () {
0 commit comments