|
11 | 11 | import zipfile
|
12 | 12 | from functools import partial
|
13 | 13 | from pathlib import Path
|
| 14 | +from typing import cast |
14 | 15 | from typing import Dict
|
15 | 16 | from typing import List
|
16 | 17 | from typing import Mapping
|
|
20 | 21 | import _pytest._code
|
21 | 22 | import pytest
|
22 | 23 | from _pytest.assertion import util
|
| 24 | +from _pytest.assertion.rewrite import _DEFAULT_REPR_MAX_SIZE |
23 | 25 | from _pytest.assertion.rewrite import _get_assertion_exprs
|
| 26 | +from _pytest.assertion.rewrite import _get_maxsize_for_saferepr |
24 | 27 | from _pytest.assertion.rewrite import AssertionRewritingHook
|
25 | 28 | from _pytest.assertion.rewrite import get_cache_dir
|
26 | 29 | from _pytest.assertion.rewrite import PYC_TAIL
|
27 | 30 | from _pytest.assertion.rewrite import PYTEST_TAG
|
28 | 31 | from _pytest.assertion.rewrite import rewrite_asserts
|
| 32 | +from _pytest.config import Config |
29 | 33 | from _pytest.config import ExitCode
|
30 | 34 | from _pytest.pathlib import make_numbered_dir
|
31 | 35 | from _pytest.pytester import Pytester
|
@@ -1708,3 +1712,47 @@ def test_foo():
|
1708 | 1712 | cache_tag=sys.implementation.cache_tag
|
1709 | 1713 | )
|
1710 | 1714 | assert bar_init_pyc.is_file()
|
| 1715 | + |
| 1716 | + |
| 1717 | +class TestReprSizeVerbosity: |
| 1718 | + """ |
| 1719 | + Check that verbosity also controls the string length threshold to shorten it using |
| 1720 | + ellipsis. |
| 1721 | + """ |
| 1722 | + |
| 1723 | + @pytest.mark.parametrize( |
| 1724 | + "verbose, expected_size", [(0, 240), (1, 240), (2, 240), (3, 2400), (4, None)] |
| 1725 | + ) |
| 1726 | + def test_get_maxsize_for_saferepr(self, verbose: int, expected_size) -> None: |
| 1727 | + class FakeConfig: |
| 1728 | + def getoption(self, name: str) -> int: |
| 1729 | + assert name == "verbose" |
| 1730 | + return verbose |
| 1731 | + |
| 1732 | + config = FakeConfig() |
| 1733 | + assert _get_maxsize_for_saferepr(cast(Config, config)) == expected_size |
| 1734 | + |
| 1735 | + def create_test_file(self, pytester: Pytester, size: int) -> None: |
| 1736 | + pytester.makepyfile( |
| 1737 | + f""" |
| 1738 | + def test_very_long_string(): |
| 1739 | + text = "x" * {size} |
| 1740 | + assert "hello world" in text |
| 1741 | + """ |
| 1742 | + ) |
| 1743 | + |
| 1744 | + @pytest.mark.parametrize("verbose_arg", ["", "-v", "-vv"]) |
| 1745 | + def test_default_verbosity(self, pytester: Pytester, verbose_arg: str) -> None: |
| 1746 | + self.create_test_file(pytester, _DEFAULT_REPR_MAX_SIZE) |
| 1747 | + result = pytester.runpytest(verbose_arg) |
| 1748 | + result.stdout.fnmatch_lines(["*xxx...xxx*"]) |
| 1749 | + |
| 1750 | + def test_increased_verbosity(self, pytester: Pytester) -> None: |
| 1751 | + self.create_test_file(pytester, _DEFAULT_REPR_MAX_SIZE) |
| 1752 | + result = pytester.runpytest("-vvv") |
| 1753 | + result.stdout.no_fnmatch_line("*xxx...xxx*") |
| 1754 | + |
| 1755 | + def test_max_increased_verbosity(self, pytester: Pytester) -> None: |
| 1756 | + self.create_test_file(pytester, _DEFAULT_REPR_MAX_SIZE * 10) |
| 1757 | + result = pytester.runpytest("-vvvv") |
| 1758 | + result.stdout.no_fnmatch_line("*xxx...xxx*") |
0 commit comments