Skip to content

Commit

Permalink
GITHUB-3196: support to execlude somes 'test' in option of command line
Browse files Browse the repository at this point in the history
  • Loading branch information
enztngh committed Jan 9, 2025
1 parent 568bfb6 commit c5b8bb1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Fixed: GITHUB-3122: Update JCommander to 1.83 (Antoine Dessaigne)
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)
Fixed: GITHUB-3140: assertEqualsDeep on Sets - Deep comparison was using the wrong expected value
Fixed: GITHUB-3189: Incorrect number of ignored tests displayed in the XML results
Fixed: GITHUB-3196: support to execlude somes 'test' in option of command line
Fixed: GITHUB-3196: support to execlude somes tests in option of command line

7.10.2
Fixed: GITHUB-3117: ListenerComparator doesn't work (Krishnan Mahadevan)
Expand Down
11 changes: 10 additions & 1 deletion testng-core-api/src/main/java/org/testng/xml/XmlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,15 @@ public XmlGroups getXmlGroups() {
* @return <code>true</code> if the current test's name matches with any of the given names.
*/
public boolean nameMatchesAny(List<String> names) {
return names.stream().anyMatch(regex -> Pattern.matches(regex, getName()));
return names.contains(getName())
|| names.stream()
.anyMatch(
regex -> {
if (regex.startsWith("/") && regex.endsWith("/")) {
String trimmedRegex = regex.substring(1, regex.length() - 1);
return Pattern.matches(trimmedRegex, getName());
}
return false;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,17 @@ public List<String> getMissedTestNames() {
List<String> missedTestNames = Lists.newArrayList();
missedTestNames.addAll(testNames);
missedTestNames.removeIf(
regex -> matchedTestNames.stream().anyMatch(testName -> Pattern.matches(regex, testName)));
regex ->
matchedTestNames.contains(regex)
|| matchedTestNames.stream()
.anyMatch(
name -> {
if (regex.startsWith("/") && regex.endsWith("/")) {
String trimmedRegex = regex.substring(1, regex.length() - 1);
return Pattern.matches(trimmedRegex, name);
}
return false;
}));
return missedTestNames;
}

Expand Down
4 changes: 2 additions & 2 deletions testng-core/src/test/java/org/testng/xml/XmlTestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public void testNameMatchesAny() {
public void testNameMatchesAnyWithRegex() {
XmlSuite xmlSuite = createDummySuiteWithTestNamesAs("test1");
XmlTest xmlTest = xmlSuite.getTests().get(0);
assertThat(xmlTest.nameMatchesAny(Collections.singletonList("^(test1$).*"))).isTrue();
assertThat(xmlTest.nameMatchesAny(Collections.singletonList("^(?!test1$).*,"))).isFalse();
assertThat(xmlTest.nameMatchesAny(Collections.singletonList("/^(test1$).*/"))).isTrue();
assertThat(xmlTest.nameMatchesAny(Collections.singletonList("/^(?!test1$).*/"))).isFalse();
}

@Test(dataProvider = "dp", description = "GITHUB-1716")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,27 @@ public void testOverrideExcludedMethodsSuiteExclusions() {
verifyTests("Failed", failed, tla.getFailedTests());
}

@Test(description = "GITHUB-2407")
public void testSpecificTestNamesWithRegexCommandLineExclusions() {
String[] args =
new String[] {
"src/test/resources/testnames/main-suite.xml",
"-log",
"0",
"-d",
OutputDirectoryPatch.getOutputDirectory(),
"-testnames",
"/^testGroup1.*/"
};

TestNG.privateMain(args, tla);

String[] passed = {"sampleOutputTest1"};
String[] failed = {};
verifyTests("Passed", passed, tla.getPassedTests());
verifyTests("Failed", failed, tla.getFailedTests());
}

private void verifyTests(String title, String[] expected, List<ITestResult> found) {

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

0 comments on commit c5b8bb1

Please sign in to comment.