Skip to content

Commit

Permalink
Merge pull request #3200 from YutingZhang-A/support-to-execlude-somes…
Browse files Browse the repository at this point in the history
…-test-in-option-of-command-line

Support excluding some tests in the option of command line
  • Loading branch information
juherr authored Jan 9, 2025
2 parents 078c1e3 + c5b8bb1 commit e162179
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +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 tests in option of command line

7.10.2
Fixed: GITHUB-3117: ListenerComparator doesn't work (Krishnan Mahadevan)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,4 @@ gpg: Good signature from "Krishnan Mahadevan (krmahadevan-key) <krishnan.mahadev
For more details regarding keys please refer:
* [Verifying Signature](https://infra.apache.org/release-signing.html#verifying-signature)
* [How to Trust Imported GPG Keys](https://classroom.anir0y.in/post/blog-how-to-trust-imported-gpg-keys/)
* [How to Trust Imported GPG Keys](https://classroom.anir0y.in/post/blog-how-to-trust-imported-gpg-keys/)
12 changes: 11 additions & 1 deletion testng-core-api/src/main/java/org/testng/xml/XmlTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.testng.xml;

import java.util.*;
import java.util.regex.Pattern;
import org.testng.TestNGException;
import org.testng.collections.Lists;
import org.testng.collections.Maps;
Expand Down Expand Up @@ -624,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.contains(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
@@ -1,6 +1,7 @@
package org.testng.xml.internal;

import java.util.List;
import java.util.regex.Pattern;
import org.testng.TestNGException;
import org.testng.collections.Lists;
import org.testng.log4testng.Logger;
Expand Down Expand Up @@ -89,7 +90,18 @@ public boolean validateMissMatchedTestNames() {
public List<String> getMissedTestNames() {
List<String> missedTestNames = Lists.newArrayList();
missedTestNames.addAll(testNames);
missedTestNames.removeIf(matchedTestNames::contains);
missedTestNames.removeIf(
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
8 changes: 8 additions & 0 deletions testng-core/src/test/java/org/testng/xml/XmlTestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ public void testNameMatchesAny() {
assertThat(xmlTest.nameMatchesAny(Collections.singletonList("test2"))).isFalse();
}

@Test(description = "GITHUB-3196")
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();
}

@Test(dataProvider = "dp", description = "GITHUB-1716")
public void testNullOrEmptyParameter(Map<String, String> data) {
XmlTest test = createXmlTest("suite", "test", Issue1716TestSample.class);
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 e162179

Please sign in to comment.