Last active
December 22, 2022 01:51
-
-
Save mikegrima/770aa5ffbeb5edad343c49f969f5f9ea to your computer and use it in GitHub Desktop.
Mock Python retry decorator
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
"""Pytest fixutre for mocking out the retry decorator as found here: https://pypi.org/project/retry/.""" | |
import mock | |
from typing import Callable, Generator | |
import pytest | |
@pytest.fixture | |
def mock_retry() -> Generator[None, None, None]: | |
"""This mocks out the retry decorator so things don't retry or block.""" | |
def mock_retry_decorator(*args, **kwargs) -> Callable: | |
def retry(func: Callable) -> Callable: | |
return func | |
return retry | |
with mock.patch("retry.retry", mock_retry_decorator): | |
yield |
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
"""Some code to do stuff you want to test.""" | |
from retry import retry | |
@retry(tries=3, delay=3) | |
def some_function() -> bool: | |
# Do some risky code here... | |
return True |
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 tests for the code""" | |
from some_code import some_function | |
def test_some_function(mock_retry: None) -> None: | |
"""Tests some function -- retry has been mocked out so it won't retry!""" | |
assert some_function() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment