Output in stdout
from the capsys
fixture is different depending on whether or not -s
is used
#11203
-
I'm using My issue is that when I run tests using the Is there a way of making the Note that I have also asked this question on StackOverflow, but so far no one has been able to help. Example codeimport argparse
import pytest
class Args:
class _HelpFormatter(argparse.HelpFormatter):
def __init__(self, prog: str) -> None:
super().__init__(prog, max_help_position=50)
def __init__(self) -> None:
global_parser = argparse.ArgumentParser(add_help=False, prog='wwaz',
formatter_class=self._HelpFormatter)
global_group = global_parser.add_argument_group('Global Options')
global_group.add_argument('--help', action='help',
help='Show this help message and exit.')
global_group.add_argument('--verbose', action='store_true',
default=False, help='Whether to output verbose logs. Also whether to show verbose test output. (default: False)')
self.parser = argparse.ArgumentParser(add_help=False, prog='wwaz',
formatter_class=self._HelpFormatter, parents=[global_parser],
description='Run routine Azure actions.')
@classmethod
def parse(self, args: list=None) -> argparse.Namespace:
return Args().parser.parse_args(args)
@pytest.fixture(scope='module')
def expected():
return (
'usage: wwaz [--help] [--verbose]'
'\n\nRun routine Azure actions.'
'\n\nGlobal Options:'
'\n --help Show this help message and exit.'
'\n --verbose Whether to output verbose logs. Also whether to show verbose test output. (default: False)\n'
)
help_args = [pytest.param(['--help'], id='--help')]
@pytest.mark.parametrize('input_args', help_args)
def test_stdout(capsys, expected, input_args):
capsys.readouterr()
with pytest.raises(SystemExit):
Args.parse(input_args)
actual, _ = capsys.readouterr()
assert actual == expected Successful test runAssuming the file containing the example code is ~/src/test_argparse.py - user@computer:~/src$ pytest test_argparse.py -q -s
.
1 passed in 0.00s Unsuccessful test runAssuming the file containing the example code is ~/src/test_argparse.py - user@computer:~/src$ pytest test_argparse.py -q
F [100%]
============================================================================================= FAILURES =============================================================================================
_______________________________________________________________________________________ test_stdout[--help] ________________________________________________________________________________________
capsys = <_pytest.capture.CaptureFixture object at 0x7f6edf733520>
expected = 'usage: wwaz [--help] [--verbose]\n\nRun routine Azure actions.\n\nGlobal Options:\n --help Show this help message and exit.\n --verbose Whether to output verbose logs. Also whether to show verbose test output. (default: False)\n'
input_args = ['--help']
@pytest.mark.parametrize('input_args', help_args)
def test_stdout(capsys, expected, input_args):
capsys.readouterr()
with pytest.raises(SystemExit):
Args.parse(input_args)
actual, _ = capsys.readouterr()
> assert actual == expected
E AssertionError: assert 'usage: wwaz ...ult: False)\n' == 'usage: wwaz ...ult: False)\n'
E Skipping 192 identical leading characters in diff, use -v to show
E - rbose test output. (default: False)
E + rbose test
E + output. (default: False)
test_argparse.py:51: AssertionError
===================================================================================== short test summary info ======================================================================================
FAILED test_argparse.py::test_stdout[--help] - AssertionError: assert 'usage: wwaz ...ult: False)\n' == 'usage: wwaz ...ult: False)\n'
1 failed in 0.01s |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This is unrelated to pytest - you'll see the same behaviour if you run This is because
So you could either pass a width in your |
Beta Was this translation helpful? Give feedback.
This is unrelated to pytest - you'll see the same behaviour if you run
Args.parse()
with your code, and then look at the output ofpython3 script.py --help
(output is a terminal) vs.python3 script.py --help | cat
(output is not a terminal).This is because
argparse.HelpFormatter
usesshutil.get_terminal_size()
to get a wrapping width (if none is given), which then falls back to 80x24 when stdout is not a terminal: