Skip to content
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

Unit tests of example do not pass #24

Closed
Nxtra opened this issue Apr 26, 2020 · 3 comments
Closed

Unit tests of example do not pass #24

Nxtra opened this issue Apr 26, 2020 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@Nxtra
Copy link

Nxtra commented Apr 26, 2020

Current behaviour

I followed the instructions in the README and installed the dependencies in a virtual environment

pip install -r hello_world/requirements.txt && pip install -r requirements-dev.txt

Then I run the tests with : POWERTOOLS_TRACE_DISABLED=1 python -m pytest.

The test fails with output:
FAILED tests/unit/test_handler.py::test_lambda_handler - aws_lambda_powertools.metrics.exceptions.SchemaValidationError: Invalid format. Error: data._aws.CloudWatchMetrics[0].Namespace must be string, Invalid item: data._aws.CloudWatchMetrics[0...
( I can provide a full stacktrace if necessary)

Potential solution
I looked into base.py of the aws_lambda_powertools metrics package and found that it is looking for an env var POWERTOOLS_METRICS_NAMESPACE.
This env var is not there when running the tests.

First setting the env var and then running it solves the issue:
POWERTOOLS_TRACE_DISABLED=1 POWERTOOLS_METRICS_NAMESPACE=test python -m pytest tests -v

If my assessment is correct, I propose to update the README. If incorrect can you help me by determining what is causing this problem?

@heitorlessa
Copy link
Contributor

heitorlessa commented Apr 27, 2020 via email

@heitorlessa heitorlessa self-assigned this Apr 27, 2020
@heitorlessa heitorlessa added the bug Something isn't working label Apr 27, 2020
@heitorlessa
Copy link
Contributor

@Nxtra - Just pushed a fix to develop branch, and thank you again for flagging this and help us improve while in beta.

Updates to the README

Relevant and updated part in bold

  • Unit Tests: We recommend proceeding with the following commands in a virtual environment
    • Install deps: pip install -r hello_world/requirements.txt && pip install -r requirements-dev.txt
    • Run tests with tracing disabled and namespace set
      • POWERTOOLS_METRICS_NAMESPACE="Example" POWERTOOLS_TRACE_DISABLED=1 python -m pytest
      • Both are necessary because app.py initializes them in the global scope, since both Tracer and Metrics will be initialized and configured during import time. For unit tests, we could always patch and explicitly config but env vars do just fine for this example.

Updates to the test

We now capture the stdout, and assert if our custom middleware and metrics were correct:

def test_lambda_handler(apigw_event, mocker, capsys):
    ...

    output = capsys.readouterr()
    output = output.out.split('\n')
    stdout_one_string = '\t'.join(output)
   ...
    
    # assess custom metric was flushed in stdout/logs
    assert "SuccessfulLocations" in stdout_one_string 
    assert "ColdStart" in stdout_one_string 
    assert "UniqueMetricDimension" in stdout_one_string

    # assess our custom middleware ran
    assert "Logging response after Handler is called" in stdout_one_string
    assert "Logging event before Handler is called" in stdout_one_string

heitorlessa added a commit that referenced this issue May 16, 2020
* develop: (21 commits)
  bugfix: #32 Runtime Error for nested sync fns
  chore: renamed history to changelog dependabot
  bugfix: resolves #31 aiohttp lazy import
  chore: grammar issues
  improv: add project tenets
  Improv tracer - async support, patch, test coverage and X-Ray escape hatch (#29)
  Bugfix: "per second" metric units (#27)
  fix: #24 correct example test and docs
  chore: bump example to use 0.8.0 features
  Adopt logging best practices (#23)
  Decorator factory Feat: Create your own middleware (#17)
  chore: clean up CI workflows
  fix: CI attempt 4
  fix: CI attempt 3
  fix: CI attempt 3
  fix: CI attempt 2
  feat: add docs to CI
  chore: fix github badge typo
  chore: pypi monthly download badge
  fix: add missing single_metric example; test var name
  ...
heitorlessa referenced this issue in heitorlessa/aws-lambda-powertools-python Jun 17, 2020
@greenpau
Copy link

greenpau commented Dec 2, 2022

POWERTOOLS_METRICS_NAMESPACE="Example" POWERTOOLS_TRACE_DISABLED=1 python -m pytest

When running in tests, you could apply it to all tests inside a test case:

@patch.dict(os.environ, {
    "UNIT_TEST_ENV": "yes",
    "POWERTOOLS_METRICS_NAMESPACE": "Example",
    "POWERTOOLS_TRACE_DISABLED": "1",
})
class HandlerTestCase(BasicTestCase):
    def test_handler(self):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants