Skip to content

chore(ci): introduce tests with Nox #4537

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

Merged
merged 6 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/quality_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ jobs:
run: make mypy
- name: Test with pytest
run: make test
- name: Test dependencies with Nox
run: make nox
- name: Security baseline
run: make security-baseline
- name: Complexity baseline
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ timeline
Pre-Pull Request <br> (make pr) : Code linting
: Docs linting
: Static typing analysis
: Tests (unit|functional|perf)
: Tests (unit|functional|perf|dependencies)
: Security baseline
: Complexity baseline
: +pre-commit checks
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ test:
poetry run pytest -m "not perf" --ignore tests/e2e --cov=aws_lambda_powertools --cov-report=xml
poetry run pytest --cache-clear tests/performance

nox:
poetry run nox --error-on-external-run --reuse-venv=yes --non-interactive

test-pydanticv2:
poetry run pytest -m "not perf" --ignore tests/e2e

Expand All @@ -47,7 +50,7 @@ coverage-html:
pre-commit:
pre-commit run --show-diff-on-failure

pr: lint lint-docs mypy pre-commit test security-baseline complexity-baseline
pr: lint lint-docs mypy pre-commit test nox security-baseline complexity-baseline

build: pr
poetry build
Expand Down
58 changes: 58 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Run nox tests
#
# usage:
# poetry run nox --error-on-external-run --reuse-venv=yes --non-interactive
#
# If you want to target a specific Python version, add -p parameter

from typing import List, Optional

import nox

PREFIX_TESTS_FUNCTIONAL = "tests/functional"
PREFIX_TESTS_UNIT = "tests/unit"


def build_and_run_test(session: nox.Session, folders: List, extras: Optional[str] = "") -> None:
"""
This function is responsible for setting up the testing environment and running the test suite for specific feature.

The function performs the following tasks:
1. Installs the required dependencies for executing any test
2. If the `extras` parameter is provided, the function installs the additional dependencies
3. the function runs the pytest command with the specified folders as arguments, executing the test suite.

Parameters
----------
session: nox.Session
The current Nox session object, which is used to manage the virtual environment and execute commands.
folders: List
A list of folder paths that contain the test files to be executed.
extras: Optional[str]
A string representing additional dependencies that should be installed for the test environment.
If not provided, the function will install the project with basic dependencies
"""

# Required install to execute any test
session.install("poetry", "pytest", "pytest-mock", "pytest_socket")

# Powertools project folder is in the root
if extras:
session.install(f"./[{extras}]")
else:
session.install("./")

# Execute test in specific folders
session.run("pytest", *folders)


@nox.session()
def test_with_only_required_packages(session: nox.Session):
"""Tests that only depends for required libraries"""
# Logger
build_and_run_test(
session,
folders=[
f"{PREFIX_TESTS_FUNCTIONAL}/logger/",
],
)
88 changes: 86 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ pytest-socket = ">=0.6,<0.8"
types-redis = "^4.6.0.7"
testcontainers = { extras = ["redis"], version = "^3.7.1" }
multiprocess = "^0.70.16"
nox = "^2024.4.15"

[tool.coverage.run]
source = ["aws_lambda_powertools"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import pytest

from aws_lambda_powertools import Logger, Tracer, set_package_logger_handler
from aws_lambda_powertools import Logger, Metrics, set_package_logger_handler
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.logging.exceptions import InvalidLoggerSamplingRateError
from aws_lambda_powertools.logging.formatter import (
Expand Down Expand Up @@ -219,13 +219,14 @@ def test_package_logger_stream(stdout):
# GIVEN package logger "aws_lambda_powertools" is explicitly set with no params
set_package_logger(stream=stdout)

# WHEN Tracer is initialized in disabled mode
Tracer(disabled=True)
# WHEN we add a dimension in Metrics feature
my_metrics = Metrics(namespace="powertools")
my_metrics.add_dimension(name="dimension", value="test")

# THEN Tracer debug log statement should be logged
# THEN Metrics debug log statement should be logged
output = stdout.getvalue()
logger = logging.getLogger("aws_lambda_powertools")
assert "Tracing has been disabled" in output
assert "Adding dimension:" in output
assert logger.level == logging.DEBUG


Expand All @@ -235,10 +236,11 @@ def test_package_logger_format(capsys):
formatter = logging.Formatter("message=%(message)s")
set_package_logger(formatter=formatter)

# WHEN Tracer is initialized in disabled mode
Tracer(disabled=True)
# WHEN we add a dimension in Metrics feature
my_metrics = Metrics(namespace="powertools")
my_metrics.add_dimension(name="dimension", value="test")

# THEN Tracer debug log statement should be logged using `message=` format
# THEN Metrics debug log statement should be logged using `message=` format
output = capsys.readouterr().out
logger = logging.getLogger("aws_lambda_powertools")
assert "message=" in output
Expand Down Expand Up @@ -977,13 +979,15 @@ def test_set_package_logger_handler_with_powertools_debug_env_var(stdout, monkey
logger = logging.getLogger("aws_lambda_powertools")

# WHEN set_package_logger is used at initialization
# and any Powertools for AWS Lambda (Python) operation is used (e.g., Tracer)
# and any Powertools for AWS Lambda (Python) operation is used (e.g., Metrics add_dimension)
set_package_logger_handler(stream=stdout)
Tracer(disabled=True)

# THEN Tracer debug log statement should be logged
my_metrics = Metrics(namespace="powertools")
my_metrics.add_dimension(name="dimension", value="test")

# THEN Metrics debug log statement should be logged
output = stdout.getvalue()
assert "Tracing has been disabled" in output
assert "Adding dimension:" in output
assert logger.level == logging.DEBUG


Expand Down