diff --git a/CHANGES.txt b/CHANGES.txt index 7c9edb612..909824763 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -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) diff --git a/README.md b/README.md index 6bad5f205..e1caa10d5 100644 --- a/README.md +++ b/README.md @@ -142,4 +142,4 @@ gpg: Good signature from "Krishnan Mahadevan (krmahadevan-key) true if the current test's name matches with any of the given names. */ public boolean nameMatchesAny(List 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; + }); } } diff --git a/testng-core/src/main/java/org/testng/xml/internal/TestNamesMatcher.java b/testng-core/src/main/java/org/testng/xml/internal/TestNamesMatcher.java index 507f35869..253ef47d3 100644 --- a/testng-core/src/main/java/org/testng/xml/internal/TestNamesMatcher.java +++ b/testng-core/src/main/java/org/testng/xml/internal/TestNamesMatcher.java @@ -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; @@ -89,7 +90,18 @@ public boolean validateMissMatchedTestNames() { public List getMissedTestNames() { List 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; } diff --git a/testng-core/src/test/java/org/testng/xml/XmlTestTest.java b/testng-core/src/test/java/org/testng/xml/XmlTestTest.java index 3fd1a9cc3..d5f06c5ab 100644 --- a/testng-core/src/test/java/org/testng/xml/XmlTestTest.java +++ b/testng-core/src/test/java/org/testng/xml/XmlTestTest.java @@ -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 data) { XmlTest test = createXmlTest("suite", "test", Issue1716TestSample.class); diff --git a/testng-core/src/test/java/test/methodselectors/CommandLineTest.java b/testng-core/src/test/java/test/methodselectors/CommandLineTest.java index fcffea3ab..e6dee8263 100644 --- a/testng-core/src/test/java/test/methodselectors/CommandLineTest.java +++ b/testng-core/src/test/java/test/methodselectors/CommandLineTest.java @@ -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 found) { Assertions.assertThat(found.stream().map(ITestResult::getName).toArray(String[]::new))