Moto does not appear to be mocking aws interactions in a pytest

Question:

Say I want to mock the following:

How do I go about starting to mock this with in pytest? I could create mocked objects by creating a dummy class and the necessary attributes, but I suspect that’s the wrong approach.

Some additional details, here’s what I’m trying to test out:

But I can’t quite seem to be able to get this patched correctly. I’m trying to make it so that I can patch session and the function calls in that method

I tried using moto and got this:

But I’m running into

So it seems like it’s not mocking anything 😐

Edit:

Turns out you need to have the mocked credentials in a config and credentials file for this to work.

Answer:

I’m not sure what exactly you want, so I’ll give you something to start.

You let unittest.mock to mock everything for you, for example. (Useful reading: https://docs.python.org/3/library/unittest.mock.html)

module.py:

test_module.py:

Results of pytest run:

I would also recommend to install pytest-socket and run pytest --disable-socket to make sure your tests do not talk with network by accident.

Leave a Reply