Best Python code snippet using localstack_python
test_lambda_api.py
Source:test_lambda_api.py
...29 os.environ.get("PROVIDER_OVERRIDE_LAMBDA") != "asf", reason="Skip for non-asf provider"30)31# TODO: move this to fixtures / reconcile with other fixture usage32@pytest.fixture33def create_lambda_function_aws(34 lambda_client,35):36 lambda_arns = []37 def _create_lambda_function(**kwargs):38 def _create_function():39 resp = lambda_client.create_function(**kwargs)40 lambda_arns.append(resp["FunctionArn"])41 def _is_not_pending():42 try:43 result = (44 lambda_client.get_function(FunctionName=resp["FunctionName"])[45 "Configuration"46 ]["State"]47 != "Pending"48 )49 return result50 except Exception as e:51 LOG.error(e)52 raise53 wait_until(_is_not_pending)54 return resp55 # @AWS, takes about 10s until the role/policy is "active", until then it will fail56 # localstack should normally not require the retries and will just continue here57 return retry(_create_function, retries=3, sleep=4)58 yield _create_lambda_function59 for arn in lambda_arns:60 try:61 lambda_client.delete_function(FunctionName=arn)62 except Exception:63 LOG.debug(f"Unable to delete function {arn=} in cleanup")64# 1. run test: AWS --snapshot-update65# 2. (recommended) run test: AWS (to verify transformers and behavior)66# 3. run test: localstack67# a) use marker skip_snapshot_verify(paths=[...]) to exclude paths that do not match yet68@pytest.mark.snapshot69@pytest.mark.aws_compatible70class TestLambdaAsfApi:71 @pytest.mark.skip_snapshot_verify72 def test_basic_invoke(73 self, lambda_client, create_lambda_function_aws, lambda_su_role, snapshot74 ):75 # predefined names76 fn_name = f"ls-fn-{short_uid()}"77 fn_name_2 = f"ls-fn-{short_uid()}"78 # custom transformers -> valid for the whole test79 # doesn't matter if added before or after a "match"-instruction80 snapshot.add_transformer(snapshot.transform.lambda_api())81 # infra setup (& validations)82 with open(os.path.join(os.path.dirname(__file__), "functions/echo.zip"), "rb") as f:83 response = create_lambda_function_aws(84 FunctionName=fn_name,85 Handler="index.handler",86 Code={"ZipFile": f.read()},87 PackageType="Zip",88 Role=lambda_su_role,89 Runtime="python3.9",90 )91 snapshot.match("lambda_create_fn", response)92 with open(os.path.join(os.path.dirname(__file__), "functions/echo.zip"), "rb") as f:93 response = create_lambda_function_aws(94 FunctionName=fn_name_2,95 Handler="index.handler",96 Code={"ZipFile": f.read()},97 PackageType="Zip",98 Role=lambda_su_role,99 Runtime="python3.9",100 )101 snapshot.match("lambda_create_fn_2", response)102 get_fn_result = lambda_client.get_function(FunctionName=fn_name)103 # match -> registers they key and takes a dict as input104 # actual assert happens later (pytest-hook)105 snapshot.match("lambda_get_fn", get_fn_result)106 get_fn_result_2 = lambda_client.get_function(FunctionName=fn_name_2)107 snapshot.match("lambda_get_fn_2", get_fn_result_2)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!