Posts Tagged: testing
-
coverage.py: Why Your Coverage Report Shows 100% But Critical Paths Are Untested
Uncover why coverage.py reports 100% line coverage while missing critical branches in if/else and loops. Enable branch coverage, integrate with pytest/tox, use pragmas, generate HTML reports, and review benchmarks.
-
Debugging PytestCollectionWarning: cannot collect test class in pytest 8.0
PytestCollectionWarning cannot collect test class pytest 8.0 fix: abstract classes, staticmethod parametrize, multiple inheritance, empty Test classes. Migration guide, code examples, pytestmark.skipif(inspect.isabstract). Keywords: 'PytestCollectionWarning pytest 8', 'cannot collect test class pytest', 'pytest 8 collection changes', 'fix pytest abstract test class warning'.
-
Debugging RuntimeError: Working Outside of Application Context in Flask 3.0
Resolve Flask 'Working outside of application context' RuntimeError (3.0+): app factories, CLI, tests, extensions. Complete SQLAlchemy examples, pytest, trade-offs.
-
Flask Application Factory Pattern: When to Use create_app() vs Global app Instance
Flask application factory pattern guide: create_app() vs global app instance. Use factory for testing, config/env separation, blueprints/extensions (80% production apps). Global for quick prototypes. Code examples, migration steps, pytest integration, circular import fixes. Flask 3.0+ best practices.
-
How to Fix Flaky pytest Tests Caused by Random Order Execution with pytest-randomly
pytest flaky tests random order fix: pytest-randomly shuffles execution order + seeds random, exposes hidden deps. Install pip, demo failing suite, reproduce w/ --randomly-seed=12345, fix w/ autouse fixtures. CI: --randomly-seed=last. Benchmarks detect 80% hidden flakiness. Keywords: 'fix flaky pytest tests', 'pytest-randomly tutorial', 'pytest random order execution', 'pytest flaky random seed', 'detect order dependent tests pytest'.
-
How to Test Flask Endpoints That Require Authentication Without Hitting the Database
Flask auth endpoint testing without DB: Mock Flask-Login user_loader, pytest fixtures for test_client + fake user. Parallel-safe (500 tests/s), zero flakes/DB setup. Full app+tests code. Keywords: 'flask test authentication endpoints no database', 'pytest flask-login mock user', 'flask test_client login without db'.
-
Migrating Django Tests from unittest to pytest: 7 Patterns for Using Fixtures Instead of setUp
Migrate Django tests from unittest to pytest: Replace setUp with 7 fixture patterns for faster, parallel tests. Django 5.1 + pytest-django 5.0 examples.
-
pytest-cov: Excluding Virtual Environment and Migration Files from Coverage Reports
Configure .coveragerc to omit virtual environments (venv, .tox), migrations, and other non-source files from pytest-cov reports, focusing on application code. Includes configuration example and Django demo.
-
pytest-mock vs unittest.mock: When MagicMock Causes False Positive Test Passes
pytest-mock vs unittest.mock: MagicMock false positives hide KeyError/AttributeError bugs. pytest mocker.patch with autospec=True catches issues early. Python mocking pitfalls, migration guide, strict mocking best practices. Keywords: pytest-mock vs unittest.mock, MagicMock false positive, pytest mocker fixture, mock spec=Dict, python mocking best practices.
-
Reducing pytest Suite Runtime from 45 Minutes to 6 Minutes with pytest-xdist Parallelization
pytest-xdist parallel testing: Speed up pytest suite 45min→6min (7.5x) on multi-core CPUs. pytest -n auto detects cores, -n 8 manual. Markers for serial tests, pytest.ini config. Benchmarks Python 3.12, 1000+ tests. Handles DB fixtures.
-
tox.ini Configuration for Testing Python Packages Across 3.10‑3.13
Example tox.ini for multi‑Python testing (py310‑py313) with pytest, coverage, mypy, and ruff. Includes CI, benchmarks, and pyproject.toml integration.
-
Using pytest Fixtures with FastAPI TestClient for Database Transaction Rollbacks
FastAPI pytest fixtures TestClient database transaction rollbacks: Isolate tests, zero data pollution. SQLAlchemy async sessions, pytest-asyncio override dependency. 500+ tests/s clean state. Code examples, conftest.py fixture, session.begin()/rollback(). Keywords: 'fastapi pytest database testing', 'fastapi testclient fixtures rollback', 'pytest fastapi sqlalchemy transaction rollback', 'fastapi testing isolation', 'pytest fixtures testclient'.