From cd4e6494a8f5b15aa82a0215426abe41aee6c29b Mon Sep 17 00:00:00 2001 From: "Ilya.Kazakevich" Date: Sat, 18 Sep 2021 03:35:17 +0300 Subject: [PATCH] PY-32605: use `repr` for diff in pytest ``assert "[]" == []`` ``assert "123" == 123`` is only displayed correctly when we ``repr`` both GitOrigin-RevId: ff962dffe7341e95ceeda613e557e9de5cb2620f --- python/helpers/pycharm/_jb_pytest_runner.py | 3 ++- python/helpers/pycharm/teamcity/pytest_plugin.py | 14 +++++++++----- .../env/python/testing/PythonPyTestingTest.java | 4 ++-- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/python/helpers/pycharm/_jb_pytest_runner.py b/python/helpers/pycharm/_jb_pytest_runner.py index d371648f1f4c..27ef85b33822 100644 --- a/python/helpers/pycharm/_jb_pytest_runner.py +++ b/python/helpers/pycharm/_jb_pytest_runner.py @@ -10,6 +10,7 @@ from pkg_resources import iter_entry_points from _jb_runner_tools import jb_patch_separator, jb_doc_args, JB_DISABLE_BUFFERING, start_protocol, parse_arguments, \ set_parallel_mode from teamcity import pytest_plugin +import os if __name__ == '__main__': path, targets, additional_args = parse_arguments() @@ -46,5 +47,5 @@ if __name__ == '__main__': set_parallel_mode() start_protocol() - + os.environ["_JB_REPR_DIFF"] = "1" sys.exit(pytest.main(args, plugins_to_load + [Plugin])) diff --git a/python/helpers/pycharm/teamcity/pytest_plugin.py b/python/helpers/pycharm/teamcity/pytest_plugin.py index 7fbc7d8cce18..cdb9466c6d7e 100644 --- a/python/helpers/pycharm/teamcity/pytest_plugin.py +++ b/python/helpers/pycharm/teamcity/pytest_plugin.py @@ -12,20 +12,24 @@ tests under TeamCity build. """ import os -import sys import re +import sys import traceback from datetime import timedelta -from teamcity.messages import TeamcityServiceMessages -from teamcity.common import convert_error_to_string, dump_test_stderr, dump_test_stdout -from teamcity import is_running_under_teamcity from teamcity import diff_tools +from teamcity import is_running_under_teamcity +from teamcity.common import convert_error_to_string, dump_test_stderr, dump_test_stdout +from teamcity.messages import TeamcityServiceMessages diff_tools.patch_unittest_diff() _ASSERTION_FAILURE_KEY = '_teamcity_assertion_failure' +def _repr_if_needed(obj): + return repr(obj) if '_JB_REPR_DIFF' in os.environ else obj + + def pytest_addoption(parser): group = parser.getgroup("terminal reporting", "reporting", after="general") @@ -282,7 +286,7 @@ class EchoTeamCityMessages(object): self.report_test_finished(test_id, duration) def pytest_assertrepr_compare(self, config, op, left, right): - setattr(self.current_test_item, _ASSERTION_FAILURE_KEY, (op, left, right)) + setattr(self.current_test_item, _ASSERTION_FAILURE_KEY, (op, _repr_if_needed(left), _repr_if_needed(right))) def pytest_runtest_logreport(self, report): """ diff --git a/python/testSrc/com/jetbrains/env/python/testing/PythonPyTestingTest.java b/python/testSrc/com/jetbrains/env/python/testing/PythonPyTestingTest.java index 658fd3b5e00a..710d47f26768 100644 --- a/python/testSrc/com/jetbrains/env/python/testing/PythonPyTestingTest.java +++ b/python/testSrc/com/jetbrains/env/python/testing/PythonPyTestingTest.java @@ -116,8 +116,8 @@ public final class PythonPyTestingTest extends PyEnvTestCase { @NotNull final String stderr, @NotNull final String all, int exitCode) { - final String expectedConsoleText = "Expected :expected\n" + - "Actual :actual\n" + + final String expectedConsoleText = "Expected :'expected'\n" + + "Actual :'actual'\n" + ""; assertThat("No diff", runner.getAllConsoleText(), containsString(expectedConsoleText)); }