From 364974141d02c5edc549f3d22b188a78e6f063d1 Mon Sep 17 00:00:00 2001 From: Tanya Date: Wed, 7 Jan 2026 15:02:41 -0500 Subject: [PATCH] chore: Add pytest configuration and update CI to skip DeepFace during tests This commit introduces a new `pytest.ini` configuration file for backend tests, specifying test discovery patterns and output options. Additionally, the CI workflow is updated to set an environment variable that prevents DeepFace and TensorFlow from loading during tests, avoiding illegal instruction errors on certain CPUs. The face service and pose detection modules are modified to conditionally import DeepFace and RetinaFace based on this environment variable, enhancing test reliability. These changes improve the testing setup and contribute to a more robust CI process. --- .gitea/workflows/ci.yml | 2 ++ backend/services/face_service.py | 14 ++++++++++---- pytest.ini | 27 +++++++++++++++++++++++++++ src/utils/pose_detection.py | 14 ++++++++++---- tests/conftest.py | 4 ++++ 5 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 pytest.ini diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 6809333..8dd954f 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -374,7 +374,9 @@ jobs: - name: Run backend tests run: | export PYTHONPATH=$(pwd) + export SKIP_DEEPFACE_IN_TESTS=1 echo "🧪 Running all backend API tests..." + echo "⚠️ DeepFace/TensorFlow disabled in tests to avoid CPU instruction errors" python -m pytest tests/ -v --tb=short --cov=backend --cov-report=term-missing --cov-report=xml --junit-xml=test-results.xml || true continue-on-error: true diff --git a/backend/services/face_service.py b/backend/services/face_service.py index 6b6903f..706d348 100644 --- a/backend/services/face_service.py +++ b/backend/services/face_service.py @@ -14,11 +14,17 @@ from PIL import Image from sqlalchemy.orm import Session, joinedload from sqlalchemy import and_, func, case -try: - from deepface import DeepFace - DEEPFACE_AVAILABLE = True -except ImportError: +# Skip DeepFace import during tests to avoid illegal instruction errors +if os.getenv("SKIP_DEEPFACE_IN_TESTS") == "1": DEEPFACE_AVAILABLE = False + DeepFace = None +else: + try: + from deepface import DeepFace + DEEPFACE_AVAILABLE = True + except ImportError: + DEEPFACE_AVAILABLE = False + DeepFace = None from backend.config import ( CONFIDENCE_CALIBRATION_METHOD, diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..611e2cb --- /dev/null +++ b/pytest.ini @@ -0,0 +1,27 @@ +[pytest] +# Pytest configuration for PunimTag backend tests + +# Test discovery patterns +python_files = test_*.py +python_classes = Test* +python_functions = test_* + +# Test paths +testpaths = tests + +# Output options +addopts = + -v + --strict-markers + --tb=short + --disable-warnings + +# Markers +markers = + slow: marks tests as slow (dummy marker for future use) + integration: marks tests as integration tests (dummy marker for future use) + +# Environment variables set before test collection +# SKIP_DEEPFACE_IN_TESTS is set in conftest.py to prevent DeepFace/TensorFlow +# from loading during tests (avoids illegal instruction errors on some CPUs) + diff --git a/src/utils/pose_detection.py b/src/utils/pose_detection.py index b0b84ce..8a93829 100644 --- a/src/utils/pose_detection.py +++ b/src/utils/pose_detection.py @@ -1,15 +1,21 @@ """Face pose detection (yaw, pitch, roll) using RetinaFace landmarks""" +import os import numpy as np from math import atan2, degrees from typing import Dict, Tuple, Optional, List -try: - from retinaface import RetinaFace - RETINAFACE_AVAILABLE = True -except ImportError: +# Skip RetinaFace import during tests to avoid illegal instruction errors +if os.getenv("SKIP_DEEPFACE_IN_TESTS") == "1": RETINAFACE_AVAILABLE = False RetinaFace = None +else: + try: + from retinaface import RetinaFace + RETINAFACE_AVAILABLE = True + except ImportError: + RETINAFACE_AVAILABLE = False + RetinaFace = None class PoseDetector: diff --git a/tests/conftest.py b/tests/conftest.py index f6d4cac..5e50d50 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -10,6 +10,10 @@ from fastapi.testclient import TestClient from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker, Session +# Prevent DeepFace/TensorFlow from loading during tests (causes illegal instruction on some CPUs) +# Set environment variable BEFORE any backend imports that might trigger DeepFace/TensorFlow +os.environ["SKIP_DEEPFACE_IN_TESTS"] = "1" + from backend.app import create_app from backend.db.base import Base from backend.db.session import get_db