This document describes the testing strategy and how to run tests for the FastAPI MongoDB MFlix sample application.
The test suite is organized into three categories:
- Tests Pydantic model validation
- Validates request/response data structures
- No database or external dependencies required
- 10 tests covering
CreateMovieRequest,UpdateMovieRequest, andMoviemodels
- Tests route handler functions in isolation
- Uses
unittest.mock.AsyncMockto mock MongoDB operations - No database connection required
- Fast execution (< 2 seconds)
- 51 tests covering:
- CRUD operations (create, read, update, delete)
- Batch operations
- Search functionality
- Vector search
- Aggregation pipelines
- Tests the full HTTP request/response cycle
- Requires a running MongoDB instance with MFlix dataset
- Uses a real server running in a subprocess
- Tests are idempotent (clean up after themselves)
- 10 tests covering:
- CRUD operations
- Batch operations
- Search functionality
- Aggregation pipelines
-
For all tests:
cd server/ source .venv/bin/activate # or `.venv\Scripts\activate` on Windows
-
For integration tests only:
- MongoDB instance running with MFlix dataset loaded
- Connection string configured in
.envfile - Port 8001 available (used for test server)
pytest tests/ -vExpected output: 71 passed in ~6 seconds
pytest -m unit -vExpected output: 61 passed, 10 deselected in ~1.5 seconds
pytest -m integration -vExpected output: 10 passed, 61 deselected in ~5 seconds
# Schema tests
pytest tests/test_movie_schemas.py -v
# Unit tests
pytest tests/test_movie_routes.py -v
# Integration tests
pytest tests/integration/test_movie_routes_integration.py -v# Run a specific test class
pytest tests/test_movie_routes.py::TestCreateMovie -v
# Run a specific test method
pytest tests/test_movie_routes.py::TestCreateMovie::test_create_movie_success -vTests are marked with pytest markers for selective execution:
@pytest.mark.unit- Unit tests with mocked dependencies@pytest.mark.integration- Integration tests requiring database
The integration tests start a real FastAPI server in a subprocess because:
- Event Loop Isolation: AsyncMongoClient binds to the event loop it was created in. Using a real server avoids event loop conflicts.
- Real-World Testing: Tests the actual deployment configuration, including middleware, CORS, and startup events.
- Educational Value: Demonstrates a practical integration testing pattern for async Python applications.
All integration tests are designed to be idempotent:
- Create operations: Tests create new documents with unique identifiers
- Cleanup: Fixtures automatically delete created documents after tests
- Read-only tests: Tests against existing MFlix data don't modify anything
- Batch operations: Create and delete multiple documents with proper cleanup
Integration tests use pytest fixtures for test data lifecycle management:
client: AsyncClient connected to the test servertest_movie_data: Sample movie data for creating test documentscreated_movie: Creates a movie and cleans it up automaticallymultiple_test_movies: Creates 3 movies for batch operation testing
The test_batch_create_movies test is currently skipped due to a known bug in the API:
- Issue:
create_movies_batchfunction callsinsert_manytwice (lines 1006 and 1015 inmovies.py) - Impact: Causes 500 error on batch create operations
- Status: To be fixed in a separate PR
- Test behavior: Test detects the error and skips gracefully
Error: Port 8001 is already in use
Solution:
- Kill any process using port 8001:
lsof -ti:8001 | xargs kill -9 - Or change the port in
tests/integration/conftest.py
Error: Connection timeout or authentication error
Solution:
- Verify MongoDB is running
- Check
.envfile has correctMONGODB_URI - Ensure MFlix dataset is loaded
- Test connection:
mongosh <your-connection-string>
Error: ModuleNotFoundError
Solution:
- Ensure virtual environment is activated
- Install dependencies:
pip install -r requirements.txt - Run from
server/python-fastapidirectory
When adding new routes or functionality:
- Add unit tests in
test_movie_routes.pywith mocked dependencies - Add integration tests in
tests/integration/test_movie_routes_integration.pyfor end-to-end validation - Use appropriate markers (
@pytest.mark.unitor@pytest.mark.integration) - Follow fixture patterns for test data lifecycle management
- Ensure idempotency - tests should clean up after themselves
- Document test purpose with clear docstrings