Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jinpyojeon/d149950b64d1dd0e98035bedb67cc4ca to your computer and use it in GitHub Desktop.
Save jinpyojeon/d149950b64d1dd0e98035bedb67cc4ca 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