Skip to content

Instantly share code, notes, and snippets.

@mneil
Created November 24, 2020 21:28
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mneil/584c151c3ae239c565513f0a87c40c2b to your computer and use it in GitHub Desktop.
Save mneil/584c151c3ae239c565513f0a87c40c2b to your computer and use it in GitHub Desktop.
Patch boto3 to avoid credentials errors and real API calls during unit tests.
'''
Unit test setup automatically called
'''
from unittest import mock
import pytest
# Fixture, autouse
@pytest.fixture(scope='session', autouse=True)
def patch_boto3(request):
'''patch things for every unit test'''
# Patch boto client to return a magic mock by default during
# Avoids ever making real calls in tests or getting botocore
# credentials errors in tests
patched = mock.patch('botocore.client.BaseClient._make_api_call')
# Start the patch
patched.start()
def unpatch():
'''try to unpatch if the patch is still active'''
try:
patched.stop()
except IndexError:
pass
# When the test is over, run this method
request.addfinalizer(unpatch)
@3p3r
Copy link

3p3r commented Feb 2, 2023

elite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment