Trying to test stdout of a local method in an object that verifies users account.
As an example,
class Foo: def __init__(self, bar, baz): self.bar = bar if baz: self.something = self.load_something() else: print('Error initializing') def load_something(self): return '' def make_subprocess_call(self): return 'stdout', 'stderr' def confirm_something(self): subprocess_stdout, subprocess_stderr = self.make_subprocess_call() if subprocess_stdout: print('good output') else: print('err output', subprocess_stderr) Then to test:
from Foo_Class_File import Foo import mock import pytest @mock.patch('Foo_Class_File.Foo.load_something', return_value = {'a': 'b'}) @mock.patch('Foo_Class_File.Foo.make_subprocess_call', return_value=('more stdout', 'more_stderr')) def test_confirm_something(testdir, capsys): test_foo = Foo(testdir, True) test_foo.confirm_something() out, err = capsys.readouterr() assert out == 'more stdout' assert err == 'more_stderr' Then running:
python3 -m pytest test_foo_file.py gives:
============================================================================== FAILURES =============================================================================== _______________________________________________________________________ test_confirm_something ________________________________________________________________________ testdir = <MagicMock name='make_subprocess_call' id='140533346667632'>, capsys = <MagicMock name='load_something' id='140533346970400'> @mock.patch('Foo_Class_File.Foo.load_something', return_value = {'a': 'b'}) @mock.patch('Foo_Class_File.Foo.make_subprocess_call', return_value=('more stdout', 'more_stderr')) def test_confirm_something(testdir, capsys): test_foo = Foo(testdir, True) test_foo.confirm_something() > out, err = capsys.readouterr() E ValueError: not enough values to unpack (expected 2, got 0) blah.py:10: ValueError ------------------------------------------------------------------------ Captured stdout call ------------------------------------------------------------------------- good output ======================================================================= short test summary info ======================================================================= FAILED test_foo_file.py::test_confirm_something - ValueError: not enough values to unpack (expected 2, got 0) ========================================================================== 1 failed in 0.11s ========================================================================== I'm just really confused because it says that is capturing the stdout?
I think it has to do with instantiating the object and then calling the method, as I have no problem capturing with capsys if there is just the object, but as soon as I try to instantiate it and call a method, what you see above it what happens. I couldn't find a similar case online or in the documentation, not for lack of trying (or perhaps my google foo is weak today).
Apologies if I am doing something silly but at this point I am concerned for the safety of my keyboard and figured I would reach out. Any help/suggestions are appreciated. You can run this test case I posted, it should give you the same error as posted.