Skip to content

Instantly share code, notes, and snippets.

@mikegrima
Last active December 22, 2022 01:51
Show Gist options
  • Save mikegrima/770aa5ffbeb5edad343c49f969f5f9ea to your computer and use it in GitHub Desktop.
Save mikegrima/770aa5ffbeb5edad343c49f969f5f9ea to your computer and use it in GitHub Desktop.
Mock Python retry decorator
"""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
"""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
"""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