Hello all!
I am trying to mock a coroutine, so I can generate some simple unittests for my pipeline. However, this doesn't work as I exepct.
I have following function which is part of the dagger pipeline:
async def unittests(runner):
return await runner.with_exec(["poetry", "run", "pytest", "tests/unittests"]).stdout() # (1)
Which I want to test:
@pytest.mark.asyncio
async def test_pipeline():
await unittests(mock_runner)
assert mock_runner.with_exec.assert_called_with(["poetry", "run", "pytest", "tests/unittests"])
The problem is, that I cannot build a mock, which provides the .stdout() and therefore the test fails.
await mock_runner.with_exec().stdout()
Traceback (most recent call last):
File "<string>", line 1, in <module>
AttributeError: 'coroutine' object has no attribute 'stdout'
Any idea how the mock needs to look like?
Of course, that didn't work ...
mock_runner = mock.AsyncMock()
mock_runner.with_exec = mock.AsyncMock()
mock_runner.with_exec.stdout = mock.AsyncMock()
Using mock.MagicMock works as I expect, but does not work within the async/await statement.