Skip to content

Commit e162179

Browse files
authored
Merge pull request #3200 from YutingZhang-A/support-to-execlude-somes-test-in-option-of-command-line
Support excluding some tests in the option of command line
2 parents 078c1e3 + c5b8bb1 commit e162179

File tree

6 files changed

+55
-3
lines changed

6 files changed

+55
-3
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Fixed: GITHUB-3122: Update JCommander to 1.83 (Antoine Dessaigne)
66
Fixed: GITHUB-3135: assertEquals on arrays - Failure message is missing information about the array index when an array element is unexpectedly null or non-null (Albert Choi)
77
Fixed: GITHUB-3140: assertEqualsDeep on Sets - Deep comparison was using the wrong expected value
88
Fixed: GITHUB-3189: Incorrect number of ignored tests displayed in the XML results
9+
Fixed: GITHUB-3196: support to execlude somes tests in option of command line
910

1011
7.10.2
1112
Fixed: GITHUB-3117: ListenerComparator doesn't work (Krishnan Mahadevan)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,4 @@ gpg: Good signature from "Krishnan Mahadevan (krmahadevan-key) <krishnan.mahadev
142142
For more details regarding keys please refer:
143143
144144
* [Verifying Signature](https://infra.apache.org/release-signing.html#verifying-signature)
145-
* [How to Trust Imported GPG Keys](https://classroom.anir0y.in/post/blog-how-to-trust-imported-gpg-keys/)
145+
* [How to Trust Imported GPG Keys](https://classroom.anir0y.in/post/blog-how-to-trust-imported-gpg-keys/)

testng-core-api/src/main/java/org/testng/xml/XmlTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.testng.xml;
22

33
import java.util.*;
4+
import java.util.regex.Pattern;
45
import org.testng.TestNGException;
56
import org.testng.collections.Lists;
67
import org.testng.collections.Maps;
@@ -624,6 +625,15 @@ public XmlGroups getXmlGroups() {
624625
* @return <code>true</code> if the current test's name matches with any of the given names.
625626
*/
626627
public boolean nameMatchesAny(List<String> names) {
627-
return names.contains(getName());
628+
return names.contains(getName())
629+
|| names.stream()
630+
.anyMatch(
631+
regex -> {
632+
if (regex.startsWith("/") && regex.endsWith("/")) {
633+
String trimmedRegex = regex.substring(1, regex.length() - 1);
634+
return Pattern.matches(trimmedRegex, getName());
635+
}
636+
return false;
637+
});
628638
}
629639
}

testng-core/src/main/java/org/testng/xml/internal/TestNamesMatcher.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.testng.xml.internal;
22

33
import java.util.List;
4+
import java.util.regex.Pattern;
45
import org.testng.TestNGException;
56
import org.testng.collections.Lists;
67
import org.testng.log4testng.Logger;
@@ -89,7 +90,18 @@ public boolean validateMissMatchedTestNames() {
8990
public List<String> getMissedTestNames() {
9091
List<String> missedTestNames = Lists.newArrayList();
9192
missedTestNames.addAll(testNames);
92-
missedTestNames.removeIf(matchedTestNames::contains);
93+
missedTestNames.removeIf(
94+
regex ->
95+
matchedTestNames.contains(regex)
96+
|| matchedTestNames.stream()
97+
.anyMatch(
98+
name -> {
99+
if (regex.startsWith("/") && regex.endsWith("/")) {
100+
String trimmedRegex = regex.substring(1, regex.length() - 1);
101+
return Pattern.matches(trimmedRegex, name);
102+
}
103+
return false;
104+
}));
93105
return missedTestNames;
94106
}
95107

testng-core/src/test/java/org/testng/xml/XmlTestTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ public void testNameMatchesAny() {
2020
assertThat(xmlTest.nameMatchesAny(Collections.singletonList("test2"))).isFalse();
2121
}
2222

23+
@Test(description = "GITHUB-3196")
24+
public void testNameMatchesAnyWithRegex() {
25+
XmlSuite xmlSuite = createDummySuiteWithTestNamesAs("test1");
26+
XmlTest xmlTest = xmlSuite.getTests().get(0);
27+
assertThat(xmlTest.nameMatchesAny(Collections.singletonList("/^(test1$).*/"))).isTrue();
28+
assertThat(xmlTest.nameMatchesAny(Collections.singletonList("/^(?!test1$).*/"))).isFalse();
29+
}
30+
2331
@Test(dataProvider = "dp", description = "GITHUB-1716")
2432
public void testNullOrEmptyParameter(Map<String, String> data) {
2533
XmlTest test = createXmlTest("suite", "test", Issue1716TestSample.class);

testng-core/src/test/java/test/methodselectors/CommandLineTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,27 @@ public void testOverrideExcludedMethodsSuiteExclusions() {
170170
verifyTests("Failed", failed, tla.getFailedTests());
171171
}
172172

173+
@Test(description = "GITHUB-2407")
174+
public void testSpecificTestNamesWithRegexCommandLineExclusions() {
175+
String[] args =
176+
new String[] {
177+
"src/test/resources/testnames/main-suite.xml",
178+
"-log",
179+
"0",
180+
"-d",
181+
OutputDirectoryPatch.getOutputDirectory(),
182+
"-testnames",
183+
"/^testGroup1.*/"
184+
};
185+
186+
TestNG.privateMain(args, tla);
187+
188+
String[] passed = {"sampleOutputTest1"};
189+
String[] failed = {};
190+
verifyTests("Passed", passed, tla.getPassedTests());
191+
verifyTests("Failed", failed, tla.getFailedTests());
192+
}
193+
173194
private void verifyTests(String title, String[] expected, List<ITestResult> found) {
174195

175196
Assertions.assertThat(found.stream().map(ITestResult::getName).toArray(String[]::new))

0 commit comments

Comments
 (0)