|
| 1 | +# Run nox tests |
| 2 | +# |
| 3 | +# usage: |
| 4 | +# poetry run nox --error-on-external-run --reuse-venv=yes --non-interactive |
| 5 | +# |
| 6 | +# If you want to target a specific Python version, add -p parameter |
| 7 | + |
| 8 | +from typing import List, Optional |
| 9 | + |
| 10 | +import nox |
| 11 | + |
| 12 | +PREFIX_TESTS_FUNCTIONAL = "tests/functional" |
| 13 | +PREFIX_TESTS_UNIT = "tests/unit" |
| 14 | + |
| 15 | +PYTHON_VERSIONS = ["3.8", "3.9", "3.10", "3.11", "3.12"] |
| 16 | + |
| 17 | + |
| 18 | +def build_and_run_test(session: nox.Session, folders: List, extras: Optional[str] = "") -> None: |
| 19 | + """ |
| 20 | + This function is responsible for setting up the testing environment and running the test suite for specific feature. |
| 21 | +
|
| 22 | + The function performs the following tasks: |
| 23 | + 1. Installs the required dependencies for executing any test |
| 24 | + 2. If the `extras` parameter is provided, the function installs the additional dependencies |
| 25 | + 3. the function runs the pytest command with the specified folders as arguments, executing the test suite. |
| 26 | +
|
| 27 | + Parameters |
| 28 | + ---------- |
| 29 | + session: nox.Session |
| 30 | + The current Nox session object, which is used to manage the virtual environment and execute commands. |
| 31 | + folders: List |
| 32 | + A list of folder paths that contain the test files to be executed. |
| 33 | + extras: Optional[str] |
| 34 | + A string representing additional dependencies that should be installed for the test environment. |
| 35 | + If not provided, the function will install the project with basic dependencies |
| 36 | + """ |
| 37 | + |
| 38 | + # Required install to execute any test |
| 39 | + session.install("poetry", "pytest", "pytest-mock", "pytest_socket") |
| 40 | + |
| 41 | + # Powertools project folder is in the root |
| 42 | + if extras: |
| 43 | + session.install(f"./[{extras}]") |
| 44 | + else: |
| 45 | + session.install("./") |
| 46 | + |
| 47 | + # Execute test in specific folders |
| 48 | + session.run("pytest", *folders) |
| 49 | + |
| 50 | + |
| 51 | +@nox.session(python=PYTHON_VERSIONS) |
| 52 | +def test_with_only_required_packages(session: nox.Session): |
| 53 | + """Tests that only depends for required libraries""" |
| 54 | + # Logger |
| 55 | + build_and_run_test( |
| 56 | + session, |
| 57 | + folders=[ |
| 58 | + f"{PREFIX_TESTS_FUNCTIONAL}/logger/", |
| 59 | + ], |
| 60 | + ) |
0 commit comments