Overview
VidaPlus API uses pytest as its testing framework, with additional support for async testing via pytest-asyncio and coverage reporting with pytest-cov. The test suite ensures API endpoints, authentication, database operations, and business logic work correctly.Test Configuration
Dependencies
The project includes these testing dependencies inpyproject.toml:
Pytest Configuration
The pytest configuration is defined inpyproject.toml:
pythonpath = "."- Adds project root to Python pathaddopts = '-p no:warnings'- Suppresses warnings during test runsasyncio_default_fixture_loop_scope = "function"- Sets async fixture scope to function level
Running Tests
Basic Test Execution
Run all tests:Using Taskipy
The project includes pre-configured test tasks using taskipy:- Runs linting (
task lint) as a pre-test step - Executes tests with coverage:
pytest -s -x --cov=vidaplus -vv - Generates HTML coverage report as a post-test step
-s- Show print statements and output during tests-x- Stop on first test failure--cov=vidaplus- Generate coverage report for thevidapluspackage-vv- Very verbose output
Running Specific Tests
Coverage Reports
Generate coverage report:Test Structure
Test Directory Layout
Fixtures (conftest.py)
Thetests/conftest.py file contains shared fixtures used across all tests:
session
Provides an in-memory SQLite database for isolated testing
client
FastAPI TestClient with dependency overrides for the session
paciente_user
Creates a test patient user with hashed password
profissional_user
Creates a test healthcare professional user
admin_user
Creates a test admin user with superuser permissions
token_pacient
Generates JWT token for patient authentication
token_profissional
Generates JWT token for professional authentication
token_admin
Generates JWT token for admin authentication
User Fixtures
The conftest includes fixtures for creating test users:Writing Tests
Authentication Tests
Example fromtests/test_auth.py:
CRUD Endpoint Tests
Example fromtests/test_paciente.py:
Error Handling Tests
Best Practices
Running Tests in CI/CD
For continuous integration, use this GitHub Actions example:Troubleshooting
Import Errors
If you encounter import errors, ensure:pythonpath = "."is set inpyproject.toml- You’re running tests from the project root
- All
__init__.pyfiles exist
Async Tests Failing
For async test functions, use thepytest.mark.asyncio decorator:
Database Lock Errors
SQLite in-memory databases useStaticPool to prevent threading issues. If you still encounter lock errors, ensure you’re not accessing the database outside the session context.
Test Discovery Issues
Pytest discovers tests by:- Files starting with
test_or ending with_test.py - Functions starting with
test_ - Classes starting with
Test
Next Steps
Contributing
Learn how to contribute to VidaPlus API
API Reference
Explore the complete API documentation