diff --git a/python/src/com/jetbrains/python/testing/universalTests/PyUniversalNoseTest.kt b/python/src/com/jetbrains/python/testing/universalTests/PyUniversalNoseTest.kt index 606938cbb1cb..e66a0f6c9fad 100644 --- a/python/src/com/jetbrains/python/testing/universalTests/PyUniversalNoseTest.kt +++ b/python/src/com/jetbrains/python/testing/universalTests/PyUniversalNoseTest.kt @@ -51,7 +51,7 @@ class PyUniversalNoseTestConfiguration(project: Project, factory: PyUniversalNos override fun createConfigurationEditor(): SettingsEditor = PyUniversalNoseTestSettingsEditor(this) - override fun getCustomRawArgumentsString(): String = + override fun getCustomRawArgumentsString(forRerun: Boolean): String = when { regexPattern.isEmpty() -> "" else -> "-m $regexPattern" diff --git a/python/src/com/jetbrains/python/testing/universalTests/PyUniversalPyTest.kt b/python/src/com/jetbrains/python/testing/universalTests/PyUniversalPyTest.kt index ce84527cfccb..3e2911a42ca4 100644 --- a/python/src/com/jetbrains/python/testing/universalTests/PyUniversalPyTest.kt +++ b/python/src/com/jetbrains/python/testing/universalTests/PyUniversalPyTest.kt @@ -49,7 +49,7 @@ class PyUniversalPyTestConfiguration(project: Project, factory: PyUniversalPyTes override fun createConfigurationEditor(): SettingsEditor = PyUniversalPyTestSettingsEditor(this) - override fun getCustomRawArgumentsString(): String = + override fun getCustomRawArgumentsString(forRerun: Boolean): String = when { keywords.isEmpty() -> "" else -> "-k $keywords" diff --git a/python/src/com/jetbrains/python/testing/universalTests/PyUniversalTests.kt b/python/src/com/jetbrains/python/testing/universalTests/PyUniversalTests.kt index 737262664aca..6cbd9a24116d 100644 --- a/python/src/com/jetbrains/python/testing/universalTests/PyUniversalTests.kt +++ b/python/src/com/jetbrains/python/testing/universalTests/PyUniversalTests.kt @@ -478,7 +478,7 @@ abstract class PyUniversalTestConfiguration(project: Project, locations.map { it.first }.distinctBy { it.psiElement }.map { getTestSpecForPythonTarget(it) }.filterNotNull().forEach { result.addAll(it) } - return result + generateRawArguments() + return result + generateRawArguments(true) } fun getTestSpec(): List { @@ -488,8 +488,8 @@ abstract class PyUniversalTestConfiguration(project: Project, /** * raw arguments to be added after "--" and passed to runner directly */ - private fun generateRawArguments(): List { - val rawArguments = additionalArguments + " " + getCustomRawArgumentsString() + private fun generateRawArguments(forRerun: Boolean = false): List { + val rawArguments = additionalArguments + " " + getCustomRawArgumentsString(forRerun) if (rawArguments.isNotBlank()) { return listOf("--") + getParsedAdditionalArguments(project, rawArguments) } @@ -514,7 +514,7 @@ abstract class PyUniversalTestConfiguration(project: Project, /** * @return configuration-specific arguments */ - protected open fun getCustomRawArgumentsString() = "" + protected open fun getCustomRawArgumentsString(forRerun: Boolean = false) = "" fun reset() { target.target = DEFAULT_PATH diff --git a/python/src/com/jetbrains/python/testing/universalTests/PyUniversalUnitTest.kt b/python/src/com/jetbrains/python/testing/universalTests/PyUniversalUnitTest.kt index 118b53759490..eadc23aa57d3 100644 --- a/python/src/com/jetbrains/python/testing/universalTests/PyUniversalUnitTest.kt +++ b/python/src/com/jetbrains/python/testing/universalTests/PyUniversalUnitTest.kt @@ -54,11 +54,12 @@ class PyUniversalUnitTestConfiguration(project: Project, factory: PyUniversalUni override fun createConfigurationEditor(): SettingsEditor = PyUniversalUnitTestSettingsEditor(this) - override fun getCustomRawArgumentsString(): String { + override fun getCustomRawArgumentsString(forRerun: Boolean): String { // Pattern can only be used with folders ("all in folder" in legacy terms) if ((!pattern.isNullOrEmpty()) && target.targetType != TestTargetType.CUSTOM) { val path = LocalFileSystem.getInstance().findFileByPath(target.target) ?: return "" - return if (path.isDirectory) "-p $pattern" else "" + // "Pattern" works only for "discovery" mode and for "rerun" we are using "python" targets ("concrete" tests) + return if (path.isDirectory && !forRerun) "-p $pattern" else "" } else { return "" diff --git a/python/testData/testRunner/env/unit/patternRerun/test_test.py b/python/testData/testRunner/env/unit/patternRerun/test_test.py new file mode 100644 index 000000000000..2ce9528cc1bb --- /dev/null +++ b/python/testData/testRunner/env/unit/patternRerun/test_test.py @@ -0,0 +1,9 @@ +from unittest import TestCase + + +class FooTest(TestCase): + def test_test(self): + pass + + def test_2_test(self): + self.fail() \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/env/python/testing/PythonUnitTestingTest.java b/python/testSrc/com/jetbrains/env/python/testing/PythonUnitTestingTest.java index 281fef011f51..6390ebfb7c28 100644 --- a/python/testSrc/com/jetbrains/env/python/testing/PythonUnitTestingTest.java +++ b/python/testSrc/com/jetbrains/env/python/testing/PythonUnitTestingTest.java @@ -60,6 +60,40 @@ public final class PythonUnitTestingTest extends PyEnvTestCase { }); } + /** + * Make sure test rerun works when pattern is enabled (PY-23416) + */ + @Test + public void testPatternRerun() throws Exception { + runPythonTest(new PyUnitTestProcessWithConsoleTestTask("testRunner/env/unit/patternRerun", ".") { + + @NotNull + @Override + protected PyUnitTestProcessRunner createProcessRunner() throws Exception { + // Full pass is required because it is folder + return new PyUnitTestProcessRunner(toFullPath(myScriptName), 2) { + @Override + protected void configurationCreatedAndWillLaunch(@NotNull final PyUniversalUnitTestConfiguration configuration) throws IOException { + super.configurationCreatedAndWillLaunch(configuration); + configuration.setPattern("test*"); + } + }; + } + + @Override + protected void checkTestResults(@NotNull final PyUnitTestProcessRunner runner, + @NotNull final String stdout, + @NotNull final String stderr, + @NotNull final String all) { + if (runner.getCurrentRerunStep() == 0) { + Assert.assertEquals(stderr, 2, runner.getAllTestsCount()); + } + else { + Assert.assertEquals(stderr, 1, runner.getAllTestsCount()); + } + } + }); + } /** * Ensures that python target pointing to module works correctly