Testing
This guide covers testing practices for digitalNXT Agency.
Running Tests
Basic Test Execution
# Run all tests with coverage
just test
# Run tests with verbose output
poetry run pytest -v
# Run specific test file
poetry run pytest tests/test_example.py
# Run tests matching a pattern
poetry run pytest -k "test_function_name"
Coverage Reports
# Run tests with coverage (included in just test)
just test
# Generate HTML coverage report
poetry run coverage html
Test Structure
Test Organization
tests/
├── agency/ # Tests for the agency package, mirroring the same folder structure as the package it tests
├── resources/ # Files used in testing if any like .txt, .csv, .pdf, etc.
├── src # Any Python code used in testing not part of the main package
├── conftest.py # Shared fixtures
├── mocks.py # Dedicated mocks used throughout multiple tests
└── paths.py # Singletons to enforce proper test path handling
Test Types
- Unit Tests: Test individual functions and classes
- Integration Tests: Test component interactions
- End-to-End Tests: Test complete workflows
Writing Tests
Basic Test Example
import pytest
from agency.module import function_to_test
def test_function_behavior():
"""Test that function works as expected."""
result = function_to_test("input")
assert result == "expected_output"
@pytest.fixture
def sample_data():
"""Provide test data."""
return {"key": "value"}
def test_with_fixture(sample_data):
"""Test using a fixture."""
assert sample_data["key"] == "value"
Testing Best Practices
- Write descriptive test names
- Use fixtures for reusable test data
- Use mocks to mock complex behavior or functionality that is depending on external resources like databases or cloud resources etc.
- Test both success and failure cases
- Keep tests independent and isolated
- Aim for high code coverage
Continuous Integration
Tests are automatically run by CI/CD on:
- Code commits pushed to Azure DevOps
- Pull request creation
- Merge commits
Next Steps
- Local Development - Development workflow
- Configuration - Environment setup