Skip to content

Commit 422f890

Browse files
authored
Improve test-local pytest phase reporting (#389)
1 parent 92fce34 commit 422f890

4 files changed

Lines changed: 76 additions & 15 deletions

File tree

.github/workflows/ci-test.yml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ jobs:
9090
#CACHIER_TEST_DB: "dummy_db"
9191
#CACHIER_TEST_USERNAME: "myuser"
9292
#CACHIER_TEST_PASSWORD: "yourpassword"
93-
CACHIER_TEST_VS_DOCKERIZED_MONGO: "true"
9493
CACHIER_TEST_REDIS_HOST: "localhost"
9594
CACHIER_TEST_REDIS_PORT: "6379"
9695
CACHIER_TEST_REDIS_DB: "0"
@@ -137,7 +136,7 @@ jobs:
137136
# prefix-key: "mongo-db"
138137
# images: mongo:8
139138
- name: Start MongoDB in docker
140-
if: matrix.backend == 'mongodb'
139+
if: matrix.backend == 'mongodb' && runner.os != 'Windows'
141140
run: |
142141
# start MongoDB in a container
143142
docker run -d -p ${{ env.CACHIER_TEST_PORT }}:27017 --name mongodb mongo:8
@@ -146,9 +145,18 @@ jobs:
146145
# show running containers
147146
docker ps -a
148147
149-
- name: Unit tests (DB)
150-
if: matrix.backend == 'mongodb'
148+
- name: Unit tests (MongoDB, Docker)
149+
if: matrix.backend == 'mongodb' && runner.os != 'Windows'
150+
env:
151+
CACHIER_TEST_VS_DOCKERIZED_MONGO: "true"
151152
run: pytest -m "mongo" --cov=cachier --cov-report=term --cov-report=xml:cov.xml
153+
154+
- name: Unit tests (MongoDB, in-memory)
155+
if: matrix.backend == 'mongodb' && runner.os == 'Windows'
156+
env:
157+
CACHIER_TEST_VS_DOCKERIZED_MONGO: "false"
158+
run: pytest -m "mongo" --cov=cachier --cov-report=term --cov-report=xml:cov.xml
159+
152160
- name: Speed eval
153161
run: python tests/speed_eval.py
154162

README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -853,11 +853,11 @@ This script automatically handles Docker container lifecycle, environment variab
853853
# Clean up
854854
docker stop cachier-test-mongo && docker rm cachier-test-mongo
855855
856-
**CI Environment:** The ``CACHIER_TEST_VS_DOCKERIZED_MONGO`` environment variable is set to ``True`` in the GitHub Actions CI environment, which runs tests against a real MongoDB instance on every commit and pull request.
856+
**CI Environment:** GitHub Actions runs MongoDB tests against a real Dockerized MongoDB instance on Linux. Windows MongoDB jobs use the in-memory MongoDB backend because GitHub-hosted Windows runners do not provide a stable Docker daemon for Linux MongoDB containers.
857857

858-
Contributors are encouraged to test against a real MongoDB instance before submitting PRs to ensure compatibility, though the in-memory MongoDB instance serves as a good proxy for most development.
858+
Contributors are encouraged to test against a real MongoDB instance before submitting PRs to ensure compatibility, though the in-memory MongoDB instance serves as a good proxy for most development and CI platform coverage.
859859

860-
**HOWEVER, the tests run against a live MongoDB instance when you submit a PR are the determining tests for deciding whether your code functions correctly against MongoDB.**
860+
**HOWEVER, only the Linux MongoDB CI jobs are live-server integration tests. Windows MongoDB CI jobs still exercise the Mongo backend API surface, but they are compatibility coverage over ``pymongo_inmemory`` rather than proof of live MongoDB server behavior.**
861861

862862

863863
Testing all backends locally

scripts/test-local.sh

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,28 @@ check_dependencies() {
312312
print_message $GREEN "All required dependencies are installed!"
313313
}
314314

315+
run_pytest_phase() {
316+
local phase_name="$1"
317+
shift
318+
319+
print_message $BLUE "Running $phase_name: $(printf '%q ' "$@")"
320+
321+
local phase_exit_code=0
322+
if "$@"; then
323+
phase_exit_code=0
324+
else
325+
phase_exit_code=$?
326+
fi
327+
328+
if [ "$phase_exit_code" -eq 0 ]; then
329+
print_message $GREEN "$phase_name passed"
330+
else
331+
print_message $RED "$phase_name failed with exit code $phase_exit_code"
332+
fi
333+
334+
return "$phase_exit_code"
335+
}
336+
315337
# MongoDB functions
316338
start_mongodb() {
317339
print_message $YELLOW "Starting MongoDB container..."
@@ -613,23 +635,42 @@ EOF
613635
PYTEST_ARGS+=(--cov=cachier --cov-report="$COVERAGE_REPORT")
614636
SERIAL_PYTEST_ARGS+=(--cov=cachier --cov-report="$COVERAGE_REPORT" --cov-append)
615637

616-
# Print and run the command
617-
print_message $BLUE "Running: $(printf '%q ' "${PYTEST_ARGS[@]}")"
618-
"${PYTEST_ARGS[@]}"
638+
MAIN_TEST_EXIT_CODE=0
639+
SERIAL_TEST_EXIT_CODE=0
640+
MAIN_TEST_STATUS="skipped"
641+
SERIAL_TEST_STATUS="skipped"
642+
643+
if run_pytest_phase "main pytest phase" "${PYTEST_ARGS[@]}"; then
644+
MAIN_TEST_EXIT_CODE=0
645+
MAIN_TEST_STATUS="passed"
646+
else
647+
MAIN_TEST_EXIT_CODE=$?
648+
MAIN_TEST_STATUS="failed ($MAIN_TEST_EXIT_CODE)"
649+
fi
619650

620651
if [ "$run_serial_local_tests" = true ]; then
621-
print_message $BLUE "Running serial local tests (pickle, memory) with: $(printf '%q ' "${SERIAL_PYTEST_ARGS[@]}")"
622-
"${SERIAL_PYTEST_ARGS[@]}"
652+
if run_pytest_phase "serial local pytest phase" "${SERIAL_PYTEST_ARGS[@]}"; then
653+
SERIAL_TEST_EXIT_CODE=0
654+
SERIAL_TEST_STATUS="passed"
655+
else
656+
SERIAL_TEST_EXIT_CODE=$?
657+
SERIAL_TEST_STATUS="failed ($SERIAL_TEST_EXIT_CODE)"
658+
fi
623659
else
624660
print_message $BLUE "Skipping serial local tests (pickle, memory) since not requested"
625661
fi
626662

627-
TEST_EXIT_CODE=$?
663+
TEST_EXIT_CODE=0
664+
if [ "$MAIN_TEST_EXIT_CODE" -ne 0 ]; then
665+
TEST_EXIT_CODE=$MAIN_TEST_EXIT_CODE
666+
elif [ "$SERIAL_TEST_EXIT_CODE" -ne 0 ]; then
667+
TEST_EXIT_CODE=$SERIAL_TEST_EXIT_CODE
668+
fi
628669

629670
if [ $TEST_EXIT_CODE -eq 0 ]; then
630671
print_message $GREEN "All tests passed!"
631672
else
632-
print_message $RED "Some tests failed. Exit code: $TEST_EXIT_CODE"
673+
print_message $RED "Some tests failed. Main phase: $MAIN_TEST_STATUS, serial local phase: $SERIAL_TEST_STATUS"
633674
fi
634675

635676
# Exit with test status

tests/mongo_tests/helpers.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import platform
44
import sys
5+
import warnings
56
from contextlib import suppress
7+
from typing import Any
68
from urllib.parse import quote_plus
79

810
from birch import Birch # type: ignore[import-not-found]
@@ -41,6 +43,16 @@ class CfgKey:
4143
_COLLECTION_NAME = f"cachier_test_{platform.system()}_{'.'.join(map(str, sys.version_info[:3]))}"
4244

4345

46+
def _build_inmemory_mongo_client() -> Any:
47+
with warnings.catch_warnings():
48+
warnings.filterwarnings(
49+
"ignore",
50+
message="Python 3.14 will, by default, filter extracted tar archives.*",
51+
category=DeprecationWarning,
52+
)
53+
return InMemoryMongoClient()
54+
55+
4456
def _get_cachier_db_mongo_client():
4557
host = quote_plus(CFG[CfgKey.HOST])
4658
port = quote_plus(CFG[CfgKey.PORT])
@@ -55,7 +67,7 @@ def _test_mongetter():
5567
_test_mongetter.client = _get_cachier_db_mongo_client()
5668
else:
5769
print("Using in-memory MongoDB instance for testing.")
58-
_test_mongetter.client = InMemoryMongoClient()
70+
_test_mongetter.client = _build_inmemory_mongo_client()
5971
db_obj = _test_mongetter.client["cachier_test"]
6072
if _COLLECTION_NAME not in db_obj.list_collection_names():
6173
db_obj.create_collection(_COLLECTION_NAME)

0 commit comments

Comments
 (0)