pytest icon indicating copy to clipboard operation
pytest copied to clipboard

parametrize: truncate long parameters in output in `-v` mode, still display full value in `-vv`

Open nixjdm opened this issue 5 years ago • 1 comments

AFAICT this ability doesn't exist.

I have a parameterized test that sometimes receives params that are long strings. I would generally prefer verbose output, but the long strings really clutter up the output pretty badly.

For example:

strings_list = [
    # Imagine several of these
    (
        """Imagine a whole paragraph here.""",
        """Imagine a whole paragraph here.""",
    ),
]

pytest.mark.parametrize(["content", "expected"], string_list)
def test_sample(content, expected):
    ....

Testing the above with pytest -v Will print the entirety of the long strings used in string_list. Normally this is just what I want, and the output for each line fits nicely on a single line, or maybe a little spill over to two. With long params though, this creates a mess that makes it more difficult to understand the total results of the test at a glance, and needs a lot more scrolling to get through.

Nonetheless, a hint to the params would be great. I suggest, to not change current behavior, the ability to decorate or otherwise mark specific tests to affect this behavior for just that test.

Alternatives:

  • A global setting to change this behavior (a good supplement)
  • Actually changing the default behavior of -v. I'd guess this isn't a welcome option.

nixjdm avatar May 17 '19 18:05 nixjdm

I found this code in pytest's tests that I think could be modified to do what you want.

https://github.com/pytest-dev/pytest/blob/326ae0cd88f5e954c8effc2b0c986832e9caff11/testing/python/approx.py#L48-L65

Something like this:

strings_list = [
    # Imagine several of these
    (
        """Imagine a whole paragraph here.""",
        """Imagine a whole paragraph here.""",
    ),
]

@pytest.mark.parametrize(["content", "expected"], strings_list)
def test_sample(content, expected, pytestconfig):
    original_verbosity = pytestconfig.getoption("verbose")
    pytestconfig.option.verbose = 0
    try:
        ....
    finally:
        pytestconfig.option.verbose = original_verbosity

Yay295 avatar Jan 03 '23 03:01 Yay295