3

In our repo, we have some calls to s3. We never want these to execute during testing, so we mock them out in each individual test, which gets annoying. It's a lot of repeated code, and can be dangerous if a developer forgets to write the mock before running the test.

I would like to write a pytest fixture that automatically applies the mock to every testing function. That is, I want to change my code from looking like this:

test_file.py:

@mock.patch.object(S3Hook, 'write_to_s3')
def test1(_):
    # test some stuff without writing to s3

@mock.patch.object(S3Hook, 'write_to_s3')
def test2(_):
    # test some more stuff without writing to s3

to this:

conftest.py:

@pytest.fixture(scope='function', autouse=True)
def mock_out_s3(request):
    # somehow apply the mock.patch.object decorator to request.function here


test_file.py:

def test1():
    # test some stuff; the mock is automatically applied, so we won't write to s3

def test2():
    # ditto

Is this possible?

2 Answers 2

2

Posting the details of how I got this to work (based on ParthS007's answer) to help out anyone else who is trying to do the same thing in the future:

@pytest.fixture(scope='function', autouse=True)
def mock_out_s3(request):
    patcher = mock.patch.object(S3Hook, 'write_to_s3')
    patcher.start()
    request.addfinalizer(patcher.stop)
1
  • 2
    This is the pytest way; you should thus accept your own answer. Also, the fixture body can be simpler: with mock.patch.object(S3Hook, 'write_to_s3'): yield already suffice.
    – hoefling
    Jun 24, 2019 at 19:59
1

While writing these Unittests. You can do this:


Class TestClass(TestCase):

    @classmethod
    def setUpTestData(cls):
        pass

    def tearDown(self):
        self.patcher.stop()

    def setup(self):
       self.patcher = mock.patch(S3Hook, 'write_to_s3')
       mock_apply = self.patcher.start()

    def test1(self):
    # test some stuff; the mock is automatically applied, so we won't write to s3

    def test2(self):
    # ditto


You can find more detail about the patcher here: https://docs.python.org/3/library/unittest.mock.html#the-patchers

3
  • Thanks, that's an incremental improvement on the current code in that the developer will only have to remember to write code once per class. However, I'm really looking for a project-level solution so that there is no danger of developers forgetting to do this on a new class of testing code. That's why I would prefer to use a global pytest fixture with autouse=True as in my original example.
    – Rachel W
    Jun 24, 2019 at 19:06
  • You can take a look here as well: stackoverflow.com/questions/47312835/…
    – ParthS007
    Jun 24, 2019 at 19:09
  • Ah! Never mind, I see I can use a patcher within a global pytest fixture. This seems to be working, thank you!!
    – Rachel W
    Jun 24, 2019 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.