Skip to content

Instantly share code, notes, and snippets.

@poros
Last active January 19, 2021 17:02
Show Gist options
  • Save poros/c9781ebe6367c7dec593 to your computer and use it in GitHub Desktop.
Save poros/c9781ebe6367c7dec593 to your computer and use it in GitHub Desktop.
Compose pytest fixtures at the same level and mock.patch
@pytest.fixture
def stream():
return mock.Mock(spec=Stream)
@pytest.fixture
def output():
return open('test.txt', 'w')
@pytest.fixture
def tailer(self, stream, output):
with mock.patch('logging.getLogger', autospec=True):
yield Tailer(stream, output, mock.sentinel.lines)
@mock.patch('consumer.read_lines', autospec=True)
def test_tailer(mock_read, tailer, stream):
stream.fetch.return_value = "test\n"
tailer.run()
mock_read.assert_called_once_with(stream, mock.sentinel.lines)
with open('test.txt', 'r') as f:
assert f.read() == "test"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment