Skip to content

New testing utility #1169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
heitorlessa opened this issue May 14, 2021 · 6 comments
Closed

New testing utility #1169

heitorlessa opened this issue May 14, 2021 · 6 comments
Labels

Comments

@heitorlessa
Copy link
Contributor

heitorlessa commented May 14, 2021

Original author: @beanaroo

Runtime: Python

Is your feature request related to a problem? Please describe

As per this discussion, creating test events for Lambda event sources is a laborious process.

Finding the correct event with default values tend to be 1/ invoking a Lambda function, 2/ logging the event, 3/ extracting the log event from CloudWatch, 4/ serializing back to native Python object, 5/ creating a Pytest fixture that can be used across tests.

NOTE: When the event source is supported by SAM CLI, this cycle can be shortened with sam local generate-event sub-command

This cycle repeats when you're trying to mock Lambda Context for libraries and middlewares that make use of it.

Describe the solution you'd like

Make tests events available in the project as Dataclasses that can be initialized with default values and changed as we see please.

from aws_lambda_powertools.utilities.testing import LambdaContext, ApiGatewayProxyEvent

import app

def test_handler():
    app.handler(event=ApiGatewayProxyEvent(), context=LambdaContext())

Describe alternatives you've considered

Create my own default Lambda Context using LambdaContext typing

import pytest
from aws_lambda_powertools.utilities.typing import LambdaContext


class MockLambdaContext(LambdaContext):

    def __init__(self):
        self._function_name = 'test-fn'
        self._memory_limit_in_mb = 128
        self._invoked_function_arn = 'arn:aws:lambda:us-east-1:12345678:function:test-fn'
        self._aws_request_id = '52fdfc07-2182-154f-163f-5f0f9a621d72'


@pytest.fixture
def lambda_context() -> LambdaContext:
    return MockLambdaContext()


def test_handler(lambda_context):
    app.handler(event={}, context=lambda_context)

If you provide guidance, is this something you'd like to contribute?

Additional context

@michaelbrewer
Copy link
Contributor

@heitorlessa any progress on this or volunteers. It would be nice to have a seperately package for this to help Powertools users as well as internal.

@heitorlessa heitorlessa transferred this issue from aws-powertools/powertools-lambda Apr 28, 2022
@heitorlessa heitorlessa added feature-request feature request need-customer-feedback Requires more customers feedback before making or revisiting a decision and removed Proposed need-rfc labels Apr 28, 2022
@OGoodness
Copy link

Might also be nice to have built in features for mocking the logger, metrics, and tracer decorators.

@heitorlessa
Copy link
Contributor Author

heitorlessa commented Jun 1, 2022 via email

@walmsles
Copy link
Contributor

This is needed and very useful. Almost need a separate project that harvests Lambda events from real AWS infrastructure and uses these "events" to generate data classes - this sounds so simple as I type it 😬.

Also need to split out much of the test components within the main repo - it is reusable. Would save people from taking parts and adapting them to their needs (like I have done here).

@heitorlessa
Copy link
Contributor Author

We're closing feature requests older than a year that haven't received enough customers +1 or that we were unable to prioritize. For newer customers interested in, please feel free to comment and we can reopen it.

On Testing, this is something we won't be able to do this year. If a newer customer sees this in 2024, please feel free to comment and we can revisit it

@heitorlessa heitorlessa closed this as not planned Won't fix, can't repro, duplicate, stale Jul 19, 2023
@github-project-automation github-project-automation bot moved this from Ideas to Coming soon in Powertools for AWS Lambda (Python) Jul 19, 2023
@github-actions
Copy link
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@leandrodamascena leandrodamascena moved this from Coming soon to Shipped in Powertools for AWS Lambda (Python) Jul 21, 2023
@dreamorosi dreamorosi added rejected and removed need-customer-feedback Requires more customers feedback before making or revisiting a decision labels Jan 22, 2025
@dreamorosi dreamorosi moved this from Shipped to Closed in Powertools for AWS Lambda (Python) Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Development

No branches or pull requests

5 participants