There are a lot of resources out there for using pytest, Moto, and botocore Stubber to write unit tests.
EDIT. I am rephrasing this question after further investigation:
I have a lambda_function
python script that I want to test with pytest and the Boto Stubber. Inside of lambda_function
I import a ssm_client from another python files (ssm_clt = boto3.client('ssm', region_name=region)
)
The problem is when I setup the pytest like this:
def test_lambda_handler(ssm_stubber):
ssm_stubber.activate()
ssm_stubber.add_response(
'get_parameters_by_path',
expected_params={'Path': 'my/ssm/parameter', 'Recursive': 'True'},
service_response={
'Parameters': [
{
'Name': 'my/ssm/parameter',
'Type': 'String',
'Value': 'my_string returned',
},
],
},
)
ssm_stubber.deactivate()
ssm_stubber.assert_no_pending_responses()
with the ssm_stubber
defined as a pytest fixture:
@pytest.fixture(autouse=True)
def ssm_stubber():
with Stubber(clients.ssm_clt) as stubber:
yield stubber
It uses the actual boto3 client and not the stubber one because I have an import statement in lambda_function
. I'm struggling with how to get past this. I'd like to not put a bunch of code in the regular lambda_function
that is only for testing.
It is almost like I need a conditional import, but to my knowledge this is bad practice.
Did I structure my project in a way that makes it almost impossible to use stubber with pytest in this way?
question from:https://stackoverflow.com/questions/65834259/overriding-boto3-client-with-stubbed-client