From 6b2ada0ab5d6bcae659f3a3d79d82fce1ec42397 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 22 Oct 2019 07:00:57 +0200 Subject: [PATCH] _get_line_with_reprcrash_message: remove duplicate prefix (#7) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before: > FAILED foo/test.py::test_bar - Failed: Database access not allowed, … After: > FAILED foo/test.py::test_bar - Database access not allowed, … --- src/_pytest/terminal.py | 10 +++++++--- testing/test_skipping.py | 4 ++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index e584fcbe84b..5abe171e38e 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -14,6 +14,7 @@ import pluggy import py from more_itertools import collapse +from wcwidth import wcswidth import pytest from _pytest import nodes @@ -990,8 +991,6 @@ def _get_pos(config, rep): def _get_line_with_reprcrash_message(config, rep, termwidth): """Get summary line for a report, trying to add reprcrash message.""" - from wcwidth import wcswidth - verbose_word = rep._get_verbose_word(config) pos = _get_pos(config, rep) @@ -1011,8 +1010,13 @@ def _get_line_with_reprcrash_message(config, rep, termwidth): i = msg.find("\n") if i != -1: msg = msg[:i] - len_msg = wcswidth(msg) + # Remove duplicate prefix, e.g. "Failed:" from pytest.fail. + implicit_prefix = verbose_word.lower() + ":" + if msg[: len(implicit_prefix)].lower() == implicit_prefix: + msg = msg[len(implicit_prefix) + 1 :] + + len_msg = wcswidth(msg) sep, len_sep = " - ", 3 max_len_msg = termwidth - len_line - len_sep if max_len_msg >= len_ellipsis: diff --git a/testing/test_skipping.py b/testing/test_skipping.py index 51b1bbdd6ac..bd28f9c0bfc 100644 --- a/testing/test_skipping.py +++ b/testing/test_skipping.py @@ -1144,6 +1144,9 @@ def test_summary_list_after_errors(testdir): import pytest def test_fail(): assert 0 + + def test_pytest_fail(): + pytest.fail("fail exc") """ ) result = testdir.runpytest("-ra") @@ -1152,6 +1155,7 @@ def test_fail(): "=* FAILURES *=", "*= short test summary info =*", "FAILED test_summary_list_after_errors.py::test_fail - assert 0", + "FAILED test_summary_list_after_errors.py::test_pytest_fail - fail exc", ] )