Created
November 24, 2020 21:28
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
elite.