From 5e0305f9d36d9e4e91f627668d14428b1ae3c3b9 Mon Sep 17 00:00:00 2001 From: "Ilya.Kazakevich" Date: Thu, 7 May 2015 14:26:46 +0300 Subject: [PATCH] Test stoud fixed for PY-12621 --- python/helpers/pycharm/pytest_teamcity.py | 1 + python/helpers/pycharm/pytestrunner.py | 6 ++- .../testData/testRunner/env/pytest/test2.py | 5 ++ .../python/testing/PythonPyTestingTest.java | 11 ++-- .../com/jetbrains/env/ut/PyUnitTestTask.java | 53 ++++++++++++++++++- 5 files changed, 70 insertions(+), 6 deletions(-) diff --git a/python/helpers/pycharm/pytest_teamcity.py b/python/helpers/pycharm/pytest_teamcity.py index 2eaa15cf37a4..8860257027d9 100644 --- a/python/helpers/pycharm/pytest_teamcity.py +++ b/python/helpers/pycharm/pytest_teamcity.py @@ -80,6 +80,7 @@ if PYVERSION > [1, 4, 0]: messages.testIgnored(name) elif report.failed: messages.testFailed(name, details=report.longrepr) + messages.testFinished(name) # We need to mark it finished even if it failed to display it at parent node elif report.when == "call": messages.testFinished(name) diff --git a/python/helpers/pycharm/pytestrunner.py b/python/helpers/pycharm/pytestrunner.py index 0725e2f11ff2..6b2bdd61f552 100644 --- a/python/helpers/pycharm/pytestrunner.py +++ b/python/helpers/pycharm/pytestrunner.py @@ -20,10 +20,13 @@ def get_plugin_manager(): from _pytest.core import PluginManager return PluginManager(load=True) +# "-s" is always required: no test output provided otherwise +args = sys.argv[1:] +args.append("-s") if "-s" not in args else None + if has_pytest: _preinit = [] def main(): - args = sys.argv[1:] _pluginmanager = get_plugin_manager() hook = _pluginmanager.hook try: @@ -38,7 +41,6 @@ if has_pytest: else: def main(): - args = sys.argv[1:] config = py.test.config try: config.parse(args) diff --git a/python/testData/testRunner/env/pytest/test2.py b/python/testData/testRunner/env/pytest/test2.py index b6c7b0887aa9..bcac55af6286 100644 --- a/python/testData/testRunner/env/pytest/test2.py +++ b/python/testData/testRunner/env/pytest/test2.py @@ -1,10 +1,15 @@ class TestPyTest: def testOne(self): + print("I am test1") assert 5 == 2*2 def testTwo(self): assert True + def testFail(self): + print("I will fail") + assert False + def testThree(): assert 4 == 2*2 diff --git a/python/testSrc/com/jetbrains/env/python/testing/PythonPyTestingTest.java b/python/testSrc/com/jetbrains/env/python/testing/PythonPyTestingTest.java index ab3dee60c5b0..6eaf2a872c02 100644 --- a/python/testSrc/com/jetbrains/env/python/testing/PythonPyTestingTest.java +++ b/python/testSrc/com/jetbrains/env/python/testing/PythonPyTestingTest.java @@ -2,11 +2,13 @@ package com.jetbrains.env.python.testing; import com.jetbrains.env.PyEnvTestCase; import com.jetbrains.env.ut.PyTestTestTask; +import org.hamcrest.Matchers; +import org.junit.Assert; /** * User : catherine */ -public class PythonPyTestingTest extends PyEnvTestCase{ +public class PythonPyTestingTest extends PyEnvTestCase { public void testPytestRunner() { runPythonTest(new PyTestTestTask("/testRunner/env/pytest", "test1.py") { @@ -24,9 +26,12 @@ public class PythonPyTestingTest extends PyEnvTestCase{ @Override public void after() { - assertEquals(8, allTestsCount()); + assertEquals(9, allTestsCount()); assertEquals(5, passedTestsCount()); - assertEquals(3, failedTestsCount()); + assertEquals(4, failedTestsCount()); + Assert.assertThat("No test stdout", getMockPrinter(findTestByName("testOne")).getStdOut(), Matchers.startsWith("I am test1")); + // Ensure test has stdout even it fails + Assert.assertThat("No stdout for fail", getMockPrinter(findTestByName("testFail")).getStdOut(), Matchers.startsWith("I will fail")); } }); } diff --git a/python/testSrc/com/jetbrains/env/ut/PyUnitTestTask.java b/python/testSrc/com/jetbrains/env/ut/PyUnitTestTask.java index 2615c75ae036..e300a1e69255 100644 --- a/python/testSrc/com/jetbrains/env/ut/PyUnitTestTask.java +++ b/python/testSrc/com/jetbrains/env/ut/PyUnitTestTask.java @@ -13,8 +13,11 @@ import com.intellij.execution.process.ProcessHandler; import com.intellij.execution.runners.ExecutionEnvironment; import com.intellij.execution.runners.ExecutionEnvironmentBuilder; import com.intellij.execution.runners.ProgramRunner; +import com.intellij.execution.testframework.AbstractTestProxy; import com.intellij.execution.testframework.Filter; +import com.intellij.execution.testframework.Printable; import com.intellij.execution.testframework.sm.runner.SMTestProxy; +import com.intellij.execution.testframework.sm.runner.ui.MockPrinter; import com.intellij.execution.testframework.sm.runner.ui.SMTRunnerConsoleView; import com.intellij.execution.testframework.sm.runner.ui.TestResultsViewer; import com.intellij.execution.ui.RunContentDescriptor; @@ -190,7 +193,7 @@ public abstract class PyUnitTestTask extends PyExecutionFixtureTestTask { * Run configuration. * * @param settings settings (if have any, null otherwise) - * @param config configuration to run + * @param config configuration to run * @throws Exception */ protected void runConfiguration(@Nullable final RunnerAndConfigurationSettings settings, @@ -271,6 +274,54 @@ public abstract class PyUnitTestTask extends PyExecutionFixtureTestTask { Assert.assertEquals(output(), 0, failedTestsCount()); } + /** + * Creates {@link MockPrinter} filled with test output. Use it to check what output test has. + * + * @param test test to fill mock printer with + * @return filled print. + */ + @NotNull + protected static MockPrinter getMockPrinter(@NotNull final Printable test) { + final MockPrinter printer = new MockPrinter(); + test.printOn(printer); + return printer; + } + + /** + * Searches for test by its name recursevly in {@link #myTestProxy} + * + * @param testName test name to find + * @return test + * @throws AssertionError if no test found + */ + @NotNull + public AbstractTestProxy findTestByName(@NotNull final String testName) { + final AbstractTestProxy test = findTestByName(testName, myTestProxy); + assert test != null : "No test found with name" + testName; + return test; + } + + /** + * Searches for test by its name recursevly in test, passed as arumuent. + * + * @param testName test name to find + * @param test root test + * @return test or null if not found + */ + @Nullable + private static AbstractTestProxy findTestByName(@NotNull final String testName, @NotNull final AbstractTestProxy test) { + if (test.getName().equals(testName)) { + return test; + } + for (final AbstractTestProxy testProxy : test.getChildren()) { + final AbstractTestProxy result = findTestByName(testName, testProxy); + if (result != null) { + return result; + } + } + return null; + } + public int failedTestsCount() { return myTestProxy.collectChildren(NOT_SUIT.and(Filter.FAILED_OR_INTERRUPTED)).size(); }