"__init__() got an unexpected keyword argument 'retry_state'". within unit tests.
I'm getting issues when attempting to unit test my code that's utilising tenacity and wondering if perhaps I'm using it wrong or there might be an issue
the function I'm trying to test is
@retry(wait=wait_fixed(ATTEMPT_TIMEOUT), stop=stop_after_attempt(EXTENDED_ATTEMPTS))
def pre_adding_health_checks(self, env, id, admin_user, admin_password):
token = self.get_auth_token(env, admin_user, admin_password)
api_url, _, _ = self.api_adapter.build_urls(env)
auth_token = f'Bearer {token}'
http_client = HTTPClient(auth_header='authorization', api_token=auth_token, url=api_url,
json_response=True)
path = f'/api/v0/ready/{id}'
res = http_client.do_request(url_path=path)
if not res.get("status"):
self.log.error(f"The status did not return OK.")
self.log.error(f"{res}")
raise Exception("The status did not return OK.")
within the unit test I'm trying to do the following
def test_tenant_pre_adding_health_checks_fails(tenant, mocker):
token_mock = mocker.patch.object(tenant_onboarding, "get_auth_token")
url_mock = mocker.patch.object(APIAdapter, "build_urls", return_value=["one", "two", "three"])
request_mock = mocker.patch.object(HTTPClient, "do_request", return_value={"status": False})
tenant.pre_adding_health_checks.retry.wait = wait_none
with pytest.raises(Exception) as exit_mock:
tenant.pre_adding_health_checks("environment", "id", "admin_user",
"admin_password")
assert "The status did not return OK." in str(
exit_mock.value)
token_mock.assert_called_once()
url_mock.assert_called_once()
request_mock.assert_called_once()
But whenever I run that I get this
> assert "The status did not return OK." in str(
exit_mock.value)
E assert 'The status did not return OK.' in "__init__() got an unexpected keyword argument 'retry_state'"
E + where "__init__() got an unexpected keyword argument 'retry_state'" = str(TypeError("__init__() got an unexpected keyword argument 'retry_state'"))
E + where TypeError("__init__() got an unexpected keyword argument 'retry_state'") = <ExceptionInfo TypeError("__init__() got an unexpected keyword argument 'retry_state'") tblen=4>.value
Is there something I'm doing wrong regarding retry_state or is there something I'm missing
I just ran into this error.
My mistake was that I used retry(retry=retry_if_exception), where of course I should have been using retry(retry_if_exception(fn)).
It doesn't seem to be matching your exact problem but I figured I'd document it let others like me know when they stumble upon this issue.
@shane-consensys i had the same issue as you. It's the line
tenant.pre_adding_health_checks.retry.wait = wait_none
I'm not exactly sure why, but here's how you can fix it. Add the decorator above your function
@mock.patch("tenant.pre_adding_health_checks.retry.sleep")
def test_tenant_pre_adding_health_checks_fails(mock_sleep, tenant, mocker):
...
and remove the line tenant.pre_adding_health_checks.retry.wait = wait_none
that'll fix it. Taken from