How to Run Tests¶
This guide explains how to run the test suite for the Real Estate Investor application.
Test Types¶
The application includes three types of tests:
- Unit Tests — Test individual functions and methods in isolation
- Integration Tests — Test interactions between components
- BDD Tests — Behavior-driven tests using Gherkin syntax
Prerequisites¶
Ensure all dependencies are installed:
Running All Tests¶
To run the complete test suite:
For a quieter output with just pass/fail counts:
For verbose output with test names:
Running Specific Test Types¶
Unit Tests Only¶
Integration Tests¶
BDD Tests¶
Running Tests for a Specific Module¶
Run tests in a specific file:
Run a specific test function:
Run tests matching a keyword:
Using Test Coverage¶
Generate a coverage report:
Generate an HTML coverage report:
Open htmlcov/index.html in your browser to view the detailed coverage report.
Running Tests with Docker Compose¶
If you're using Docker Compose:
For verbose output:
Database Configuration for Tests¶
Tests use a separate test database configured in investor_app/settings_test.py. The test database is automatically created and destroyed for each test run.
To use PostgreSQL for testing (instead of SQLite):
Running Tests in CI¶
The CI pipeline (.github/workflows/ci.yml) automatically runs tests on every push and pull request. Tests run with:
- PostgreSQL service container
- Full linting (ruff)
- Format checking (black)
- Type checking (mypy)
- Complete test suite (pytest)
Common Test Scenarios¶
Testing Financial Calculations¶
Testing Models and Database Operations¶
Testing Views and URLs¶
Debugging Test Failures¶
Show print statements during tests¶
Stop at first failure¶
Drop into debugger on failure¶
Run only failed tests from last run¶
Test Fixtures¶
Common fixtures are defined in:
core/tests/conftest.py— Shared fixtures for core app teststests_bdd/conftest.py— BDD test fixtures
Example fixtures:
user— Creates a test userproperty— Creates a test propertyrental_income— Creates rental income recordsoperating_expense— Creates expense records
Writing New Tests¶
When adding new functionality, always add corresponding tests:
- Unit tests for financial calculations and utility functions
- Integration tests for model methods and view logic
- BDD tests for user workflows and scenarios
Follow the existing test structure and naming conventions:
- Test files:
test_<module_name>.py - Test functions:
test_<specific_behavior> - Test classes:
Test<ClassName>
Best Practices¶
- Keep tests deterministic (no random data, no external API calls)
- Use factories or fixtures for test data
- Test edge cases (zero values, negative numbers, very large values)
- Ensure tests clean up after themselves
- Write descriptive test names that explain what is being tested
Troubleshooting¶
Tests fail with database errors¶
Ensure the test database can be created:
Import errors¶
Make sure you're in the project root and your virtual environment is activated:
Fixture not found¶
Check that conftest.py files are in the correct locations and fixtures are properly defined.
Next Steps¶
- Code Quality Tools — Run linters and type checkers
- CI/CD Configuration — Understanding automated testing
- Contributing Guidelines — How to contribute tests