Hi, I have just begun using pytest at a basic level and I am seeing behaviour that I do not understand.
My platform is Debian 10.9 There are 3 files involved, contents are provided below, and attached. - module_1.py passes the test as expected - module_2.py has a tiny change, and fails unexpectedly. - my_test.py runs the same test on each module Can anyone explain why the module_2.py test fails? Is it because stderr during module import is not the same as during test? Is it something to do with mutable defaults? How to investigate this? And how can I get the test to pass without changing module_2? Thanks :) #--------------------------------------------------------------------- Here is the file module_1.py: #!/usr/bin/python3 import sys def msg(*args): print(*args, file=sys.stderr) #--------------------------------------------------------------------- Here is the file module_2.py: #!/usr/bin/python3 import sys MSG_DESTINATION = sys.stderr def msg(*args): print(*args, file=MSG_DESTINATION) #--------------------------------------------------------------------- Here is the file: my_test.py #!/usr/bin/python3 import module_1 import module_2 def test__1(capsys): module_1.msg("a", "message") captured = capsys.readouterr() assert captured.err == """a message\n""" def test__2(capsys): module_2.msg("a", "message") captured = capsys.readouterr() assert captured.err == """a message\n""" #--------------------------------------------------------------------- Here is the pytest output: $ pytest-3 ======================== test session starts ========================= platform linux -- Python 3.7.3, pytest-3.10.1, py-1.7.0, pluggy-0.8.0 rootdir: /mnt/hart/home/d10/david/work/src/py/lab/pytest/capture/bug, inifile: collected 2 items my_test.py .F [100%] ============================== FAILURES ============================== ______________________________ test__2 _______________________________ capsys = <_pytest.capture.CaptureFixture object at 0x7ff6457134a8> def test__2(capsys): module_2.msg("a", "message") captured = capsys.readouterr() > assert captured.err == """a message\n""" E AssertionError: assert '' == 'a message\n' E + a message my_test.py:14: AssertionError ------------------------ Captured stderr call ------------------------ a message ================= 1 failed, 1 passed in 0.03 seconds =================
============================= test session starts ============================== platform linux -- Python 3.7.3, pytest-3.10.1, py-1.7.0, pluggy-0.8.0 rootdir: /mnt/hart/home/d10/david/work/src/py/lab/pytest/capture/bug, inifile: collected 2 items my_test.py .F [100%] =================================== FAILURES =================================== ___________________________________ test__2 ____________________________________ capsys = <_pytest.capture.CaptureFixture object at 0x7f9f8cc33278> def test__2(capsys): module_2.msg("a", "message") captured = capsys.readouterr() > assert captured.err == """a message\n""" E AssertionError: assert '' == 'a message\n' E + a message my_test.py:14: AssertionError ----------------------------- Captured stderr call ----------------------------- a message ====================== 1 failed, 1 passed in 0.03 seconds ======================
-- https://mail.python.org/mailman/listinfo/python-list