Reputation: 39
I am writing unit tests and want to disable tenacity, I've previously been able to disable tenacity when its a decorator ie:
@retry(stop=stop_after_attempt(3),wait=wait_fixed(5))
def function_that_retries(param) -> None:
<function implementation>
with:
def test_function(self):
# disable tenacity retry
function.retry.retry = tenacity.retry_if_not_result(lambda x: True)
Now I want to disable this sort of tenacity for loop:
@property
def is_ready(self) -> bool:
try:
for attempt in Retrying(stop=stop_after_delay(60), wait=wait_fixed(3)):
with attempt:
# The ping command is cheap and does not require auth.
self.client.admin.command("ping")
except RetryError:
return False
return True
while mocking that self.client.admin.command
raises a ConnectionFailure
error (i.e. I don't want to get around this by raising a RetryError
for the self.client.admin.command
)
Right now my test looks like this
class TestMongoServer(unittest.TestCase):
@patch("lib.charms.mongodb_libs.v0.mongodb.MongoClient")
@patch("lib.charms.mongodb_libs.v0.mongodb.MongoDBConfiguration")
def test_is_ready_error_handling(self, config, mock_client):
# TODO figure out how to disable tenacity for faster unit testing
with MongoDBConnection(config) as mongo:
mock_client.return_value.admin.command.side_effect = ConnectionFailure()
# verify ready is false when an error occurs
ready = mongo.is_ready
self.assertEqual(ready, False)
# verify we close connection
(mock_client.return_value.close).assert_called()
but it doesn't disable tenacity, whats the correct way to disable tenacity when its used with for attempt in Retrying
?
Upvotes: 0
Views: 1579
Reputation: 559
It's recommended to include tenacity for your test. just mock sleep of tenacity like:
@pytest.fixture(autouse=True)
def tenacity_wait(mocker):
mocker.patch('tenacity.nap.time')
You can add this to conftest.py in the root folder of unit test.
Upvotes: 1