From f05814daef439c5c186d3b0e4a7a1ae02de92107 Mon Sep 17 00:00:00 2001 From: Pavel Tisnovsky Date: Sun, 18 May 2025 15:20:33 +0200 Subject: [PATCH 1/4] Deps for unit tests --- pyproject.toml | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 1f503300..080c531d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,8 +22,17 @@ distribution = true [dependency-groups] dev = [ - "black>=25.1.0" - ] + "black>=25.1.0", + "pytest>=8.3.2", + "pytest-cov>=5.0.0", +] [tool.pdm.scripts] start = "pdm run make run" +test-unit = "pdm run make test-unit" +test-integration = "pdm run make test-integration" + +[tool.pytest.ini_options] +pythonpath = [ + "src" +] From edd9ced7ada7b7577b57f7cd79053d7134d883f1 Mon Sep 17 00:00:00 2001 From: Pavel Tisnovsky Date: Sun, 18 May 2025 15:20:58 +0200 Subject: [PATCH 2/4] Makefile targets to run unit and integration tests --- Makefile | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Makefile b/Makefile index 073b5888..c720aeab 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,20 @@ +ARTIFACT_DIR := $(if $(ARTIFACT_DIR),$(ARTIFACT_DIR),tests/test_results) PATH_TO_PLANTUML := ~/bin run: ## Run the service locally python src/lightspeed-stack.py +test-unit: ## Run the unit tests + @echo "Running unit tests..." + @echo "Reports will be written to ${ARTIFACT_DIR}" + COVERAGE_FILE="${ARTIFACT_DIR}/.coverage.unit" pdm run pytest tests/unit --cov=src --cov-report term-missing --cov-report "json:${ARTIFACT_DIR}/coverage_unit.json" --junit-xml="${ARTIFACT_DIR}/junit_unit.xml" --cov-fail-under=60 + +test-integration: ## Run integration tests tests + @echo "Running integration tests..." + @echo "Reports will be written to ${ARTIFACT_DIR}" + COVERAGE_FILE="${ARTIFACT_DIR}/.coverage.integration" pdm run pytest tests/integration --cov=src --cov-report term-missing --cov-report "json:${ARTIFACT_DIR}/coverage_integration.json" --junit-xml="${ARTIFACT_DIR}/junit_integration.xml" --cov-fail-under=60 + check-types: ## Checks type hints in sources MYPYPATH=src pdm run mypy --namespace-packages --explicit-package-bases --strict --disallow-untyped-calls --disallow-untyped-defs --disallow-incomplete-defs . From 552bc2ca4f7cee850df56062e4e9dad857c51fba Mon Sep 17 00:00:00 2001 From: Pavel Tisnovsky Date: Sun, 18 May 2025 15:21:56 +0200 Subject: [PATCH 3/4] Basic unit tests for log.py --- tests/unit/test_log.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 tests/unit/test_log.py diff --git a/tests/unit/test_log.py b/tests/unit/test_log.py new file mode 100644 index 00000000..cb139e18 --- /dev/null +++ b/tests/unit/test_log.py @@ -0,0 +1,15 @@ +"""Unit tests for functions defined in src/log.py.""" + + +from src.log import get_logger + + +def test_get_logger(): + """Check the function to retrieve logger.""" + logger_name = "foo" + logger = get_logger(logger_name) + assert logger is not None + assert logger.name == logger_name + + # at least one handler need to be set + assert len(logger.handlers) >= 1 From 6a5ce5437bf1f0127e39cea4133eec7179d02ca2 Mon Sep 17 00:00:00 2001 From: Pavel Tisnovsky Date: Sun, 18 May 2025 15:22:19 +0200 Subject: [PATCH 4/4] Stub for configuration tests --- tests/unit/test_configuration.py | 8 ++++++++ tests/unit/test_log.py | 1 - 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 tests/unit/test_configuration.py diff --git a/tests/unit/test_configuration.py b/tests/unit/test_configuration.py new file mode 100644 index 00000000..9b80a9e5 --- /dev/null +++ b/tests/unit/test_configuration.py @@ -0,0 +1,8 @@ +"""Unit tests for functions defined in src/configuration.py.""" + +from src.configuration import configuration + + +def test_default_configuration(): + cfg = configuration + assert cfg is not None diff --git a/tests/unit/test_log.py b/tests/unit/test_log.py index cb139e18..c0b84966 100644 --- a/tests/unit/test_log.py +++ b/tests/unit/test_log.py @@ -1,6 +1,5 @@ """Unit tests for functions defined in src/log.py.""" - from src.log import get_logger