我想声明用 pytest-bdd 将某些内容写入标准输出。这是我的stdout.feature
文件:
Feature: test writing to stdout We are testing stdout capturing Scenario: Trying to capture stdout Given we have written to stdout Then the result can be captured via capsys
这是对应的test_stdout.py
:
from pytest_bdd import scenario, given, then @scenario('stdout.feature', 'Trying to capture stdout') def test_stdout_capture(capsys):: pass @given('we have written to stdout') def test_stdout_write(capsys): print('Hello') @then('the result can be captured via capsys') def test_written_to_stdout(capsys): out, err = capsys.readouterr() assert out == 'Hello\n'
然而,当运行pytest -v
时,我得到一个奇怪的断言错误:
... collected 2 items test_stdout.py::test_stdout_capture PASSED test_stdout.py::test_written_to_stdout FAILED ... > assert out == 'Hello\n' E assert '' == 'Hello\n' E + Hello test_stdout.py:16: AssertionError
似乎test_stdout_capture
已经执行test_written_to_stdout
,其中stdout的内容丢失,就像我更改断言(即。assert out == 'Goodbye'
)时,我得到另一个断言错误。< / p>
可能导致这种情况的原因是什么?