# ForensicTrails Test Suite ## Overview This directory contains comprehensive unit and integration tests for the ForensicTrails application. ## Test Summary - **Total Tests:** 54 - **Test Files:** 4 - **Overall Coverage:** 87% - **Execution Time:** ~0.17s - **Status:** ✅ All tests passing ## Running Tests ### Run all tests ```bash python -m pytest tests/ ``` ### Run specific test file ```bash python -m pytest tests/test_database.py python -m pytest tests/test_config.py python -m pytest tests/test_logging.py python -m pytest tests/test_case_manager.py ``` ### Run with verbose output ```bash python -m pytest tests/ -v ``` ### Run with coverage report ```bash python -m pytest tests/ --cov=forensictrails --cov-report=term-missing ``` ### Generate HTML coverage report ```bash python -m pytest tests/ --cov=forensictrails --cov-report=html # Open htmlcov/index.html in your browser ``` ### Run specific test class or method ```bash python -m pytest tests/test_database.py::TestGetDbConnection python -m pytest tests/test_case_manager.py::TestCaseManager::test_create_case ``` ## Test Files ### 1. `test_config.py` (8 tests) Tests for the configuration module (`forensictrails.utils.config`). **Coverage: 100%** ✓ #### Test Classes - **TestConfig** (7 tests) - Loading from valid JSON file - Handling non-existent files - Partial configuration with defaults - Empty configuration files - Extra keys in configuration - Default constructor behavior - **TestConfigDataTypes** (1 test) - String and integer value handling ### 2. `test_logging.py` (9 tests) Tests for the logging setup module (`forensictrails.utils.logging`). **Coverage: 100%** ✓ #### Test Classes - **TestSetupLogging** (8 tests) - Log file creation - Nested directory creation - Handler addition (FileHandler, StreamHandler) - Log level configuration - Message logging - Formatter verification - Config fallback behavior - Multiple log level testing - **TestLoggingIntegration** (1 test) - Multiple message logging integration ### 3. `test_database.py` (16 tests) Comprehensive tests for the database module (`forensictrails.db.database`). **Coverage: 94%** ✓ #### Test Classes - **TestGetDbConnection** (2 tests) - Connection creation and file creation - **TestValidateDatabaseSchema** (4 tests) - Empty database validation - Incomplete database validation - Complete database validation - Non-existent database validation - **TestCreateFreshDatabase** (4 tests) - Database file creation - All required tables creation - Return value verification - Clean path behavior - **TestInitializeDatabase** (3 tests) - New database creation - Valid database preservation - Invalid database recreation - **TestShowDbSchema** (2 tests) - Logging behavior - Exception handling - **TestDatabaseIntegration** (1 test) - Complete workflow: create → use → corrupt → recreate ### 4. `test_case_manager.py` (21 tests) Comprehensive tests for the case manager module (`forensictrails.core.case_manager`). **Coverage: 97%** ✓ #### Test Classes - **TestCaseManager** (19 tests) - Initialization - Case creation (full and minimal) - Getting cases (existent and non-existent) - Listing cases (empty, multiple, filtered) - Search functionality - Combined filters - Case updates (valid, invalid, non-existent) - Status changes (close, archive) - Case deletion - Modified timestamp tracking - **TestCaseManagerIntegration** (2 tests) - Full case lifecycle integration - Multiple cases workflow ## Test Coverage Summary | Module | Statements | Missing | Coverage | |--------|-----------|---------|----------| | `forensictrails.utils.config` | 16 | 0 | **100%** ✓ | | `forensictrails.utils.logging` | 20 | 0 | **100%** ✓ | | `forensictrails.core.case_manager` | 65 | 2 | **97%** ✓ | | `forensictrails.db.database` | 65 | 4 | **94%** ✓ | | `forensictrails.__main__` | 17 | 17 | **0%** (GUI entry) | | **TOTAL** | **183** | **23** | **87%** | ### Uncovered Lines #### database.py (4 lines) - Line 10: Config fallback in `get_db_connection()` (uses mocked config) - Line 48: Config fallback in `create_fresh_database()` (uses explicit paths) - Line 69: Config fallback in `initialize_database()` (uses explicit paths) - Line 91: Debug logging (covered but mocked) #### case_manager.py (2 lines) - Lines 88, 93: Export/import functions (TODO - not implemented yet) #### **main**.py (17 lines) - Entry point for GUI application (requires PyQt6 GUI testing) ## Test Design Principles 1. **Isolation**: Each test uses temporary directories and cleans up after itself 2. **Independence**: Tests don't depend on each other and can run in any order 3. **Mocking**: External dependencies (config, logging) are mocked where appropriate 4. **Real Database**: Tests use actual SQLite databases to ensure realistic behavior 5. **Comprehensive**: Tests cover success paths, error paths, and edge cases 6. **Fast**: All 54 tests run in ~0.17 seconds ## Adding New Tests When adding new functionality: 1. Add unit tests for individual functions 2. Add integration tests for complex workflows 3. Ensure cleanup in `tearDown()` methods 4. Use descriptive test names that explain what is being tested 5. Mock external dependencies appropriately 6. Run coverage to ensure new code is tested 7. Aim for >90% coverage on new modules ## Dependencies Tests require: - `pytest` - Test framework - `pytest-cov` - Coverage reporting - Standard library: `unittest`, `tempfile`, `sqlite3`, `json`, `logging` Install test dependencies: ```bash pip install pytest pytest-cov ``` ## Continuous Integration These tests are designed to run in CI/CD pipelines. Example usage: ```bash # Run tests with JUnit XML output for CI python -m pytest tests/ --junitxml=test-results.xml # Run with coverage and fail if below threshold python -m pytest tests/ --cov=forensictrails --cov-fail-under=85 ``` ## Known Issues - **DeprecationWarning** in `case_manager.py:60`: Uses `datetime.utcnow()` which is deprecated. Should be updated to use `datetime.now(datetime.UTC)` in the future. ## Test Maintenance - Tests are automatically run on code changes - Coverage reports are generated in `htmlcov/` directory - Keep test coverage above 85% for the project - Review and update tests when refactoring code