From d54d7082d7ffef3b6dafef84fbf0a133a6619b5d Mon Sep 17 00:00:00 2001 From: "Ilya.Kazakevich" Date: Thu, 13 Apr 2017 00:06:20 +0300 Subject: [PATCH] PY-23673: "blockClosed" can be called on setup finish, not only on subtest. --- python/helpers/pycharm/_jb_runner_tools.py | 13 +++-- .../env/setup_teardown/test_test.py | 26 ++++++++++ .../python/testing/PythonNoseTestingTest.java | 12 +++++ .../python/testing/PythonPyTestingTest.java | 13 +++++ .../python/testing/PythonUnitTestingTest.java | 12 +++++ .../python/testing/SetupTearDownTestTask.java | 52 +++++++++++++++++++ 6 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 python/testData/testRunner/env/setup_teardown/test_test.py create mode 100644 python/testSrc/com/jetbrains/env/python/testing/SetupTearDownTestTask.java diff --git a/python/helpers/pycharm/_jb_runner_tools.py b/python/helpers/pycharm/_jb_runner_tools.py index 7b09e6a50ba6..54659293a4cf 100644 --- a/python/helpers/pycharm/_jb_runner_tools.py +++ b/python/helpers/pycharm/_jb_runner_tools.py @@ -205,12 +205,21 @@ class NewTeamcityServiceMessages(_old_service_messages): return test_name # Blocks are used for 2 cases now: - # 1) Unittest subtests + # 1) Unittest subtests (only closed, opened by subTestBlockOpened) # 2) setup/teardown (does not work, see https://github.com/JetBrains/teamcity-messages/issues/114) # def blockOpened(self, name, flowId=None): # self.testStarted(".".join(TREE_MANAGER.current_branch + [self._fix_setup_teardown_name(name)])) def blockClosed(self, name, flowId=None): + + # If _latest_subtest_result is not set or does not exist we closing setup method, not a subtest + try: + if not self._latest_subtest_result: + return + except AttributeError: + return + + # If we here that means we are closing subtest test_name = ".".join(TREE_MANAGER.current_branch) if self._latest_subtest_result == "Failure": self.testFailed(test_name) @@ -220,8 +229,6 @@ class NewTeamcityServiceMessages(_old_service_messages): self.testFinished(test_name) self._latest_subtest_result = None - - def subTestBlockOpened(self, name, subTestResult, flowId=None): self.testStarted(".".join(TREE_MANAGER.current_branch + [name])) self._latest_subtest_result = subTestResult diff --git a/python/testData/testRunner/env/setup_teardown/test_test.py b/python/testData/testRunner/env/setup_teardown/test_test.py new file mode 100644 index 000000000000..d8fa37be8d5f --- /dev/null +++ b/python/testData/testRunner/env/setup_teardown/test_test.py @@ -0,0 +1,26 @@ +from unittest import TestCase + + +class FooTest(TestCase): + def setUp(self): + print(1) + + @classmethod + def setUpClass(cls): + print(1) + + def test_test(self): + print(3) + + def test_2_test(self): + self.fail("D") + + def tearDown(self): + print("A") + + @classmethod + def tearDownClass(cls): + print("A") + + + diff --git a/python/testSrc/com/jetbrains/env/python/testing/PythonNoseTestingTest.java b/python/testSrc/com/jetbrains/env/python/testing/PythonNoseTestingTest.java index 839ee3ede14a..f68a331eb7df 100644 --- a/python/testSrc/com/jetbrains/env/python/testing/PythonNoseTestingTest.java +++ b/python/testSrc/com/jetbrains/env/python/testing/PythonNoseTestingTest.java @@ -26,6 +26,18 @@ import static org.junit.Assert.assertEquals; @EnvTestTagsRequired(tags = "nose") public final class PythonNoseTestingTest extends PyEnvTestCase { + // Ensures setup/teardown does not break anything + @Test + public void testSetupTearDown() throws Exception { + runPythonTest(new SetupTearDownTestTask(){ + @NotNull + @Override + protected PyNoseTestProcessRunner createProcessRunner() throws Exception { + return new PyNoseTestProcessRunner("test_test.py", 1); + } + }); + } + // Ensure slow test is not run when --attr="!slow" is provided @Test diff --git a/python/testSrc/com/jetbrains/env/python/testing/PythonPyTestingTest.java b/python/testSrc/com/jetbrains/env/python/testing/PythonPyTestingTest.java index b347ce6ad20d..63715238e916 100644 --- a/python/testSrc/com/jetbrains/env/python/testing/PythonPyTestingTest.java +++ b/python/testSrc/com/jetbrains/env/python/testing/PythonPyTestingTest.java @@ -37,6 +37,19 @@ import static org.junit.Assert.assertEquals; @EnvTestTagsRequired(tags = "pytest") public final class PythonPyTestingTest extends PyEnvTestCase { + + // Ensures setup/teardown does not break anything + @Test + public void testSetupTearDown() throws Exception { + runPythonTest(new SetupTearDownTestTask(){ + @NotNull + @Override + protected PyTestTestProcessRunner createProcessRunner() throws Exception { + return new PyTestTestProcessRunner("test_test.py", 1); + } + }); + } + // Ensure slow test is not run when -m "not slow" is provided @Test public void testMarkerWithSpaces() throws Exception { diff --git a/python/testSrc/com/jetbrains/env/python/testing/PythonUnitTestingTest.java b/python/testSrc/com/jetbrains/env/python/testing/PythonUnitTestingTest.java index c09eb54e7a34..2a3c3209f1a6 100644 --- a/python/testSrc/com/jetbrains/env/python/testing/PythonUnitTestingTest.java +++ b/python/testSrc/com/jetbrains/env/python/testing/PythonUnitTestingTest.java @@ -48,6 +48,18 @@ import static org.junit.Assert.assertEquals; */ public final class PythonUnitTestingTest extends PyEnvTestCase { + // Ensures setup/teardown does not break anything + @Test + public void testSetupTearDown() throws Exception { + runPythonTest(new SetupTearDownTestTask(){ + @NotNull + @Override + protected PyUnitTestProcessRunner createProcessRunner() throws Exception { + return new PyUnitTestProcessRunner("test_test.py", 1); + } + }); + } + @EnvTestTagsRequired(tags = "python3") // No subtest in py2 @Test public void testSubtest() throws Exception { diff --git a/python/testSrc/com/jetbrains/env/python/testing/SetupTearDownTestTask.java b/python/testSrc/com/jetbrains/env/python/testing/SetupTearDownTestTask.java new file mode 100644 index 000000000000..1d6cdde79222 --- /dev/null +++ b/python/testSrc/com/jetbrains/env/python/testing/SetupTearDownTestTask.java @@ -0,0 +1,52 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jetbrains.env.python.testing; + +import com.jetbrains.env.PyProcessWithConsoleTestTask; +import com.jetbrains.env.ut.PyScriptTestProcessRunner; +import com.jetbrains.python.sdkTools.SdkCreationType; +import org.jetbrains.annotations.NotNull; +import org.junit.Assert; + +/** + * Runs test with setup/teardown (script shall be test_test.py) and checks it works + * + * @author Ilya.Kazakevich + */ +abstract class SetupTearDownTestTask> extends PyProcessWithConsoleTestTask { + + SetupTearDownTestTask() { + super("testRunner/env/setup_teardown/", SdkCreationType.SDK_PACKAGES_ONLY); + } + + @Override + protected final void checkTestResults(@NotNull final T runner, + @NotNull final String stdout, + @NotNull final String stderr, + @NotNull final String all) { + + if (runner.getCurrentRerunStep() == 0) { + Assert.assertEquals(runner.getFormattedTestTree(), 1, runner.getFailedTestsCount()); + Assert.assertEquals(runner.getFormattedTestTree(), 1, runner.getPassedTestsCount()); + Assert.assertEquals(runner.getFormattedTestTree(), 2, runner.getAllTestsCount()); + } + else { + Assert.assertEquals(runner.getFormattedTestTree(), 1, runner.getFailedTestsCount()); + Assert.assertEquals(runner.getFormattedTestTree(), 0, runner.getPassedTestsCount()); + Assert.assertEquals(runner.getFormattedTestTree(), 1, runner.getAllTestsCount()); + } + } +}