3.4 KiB
3.4 KiB
Test Setup Guide
This document explains how to set up and run tests for the legal-doc-masker backend.
Test Structure
backend/
├── tests/
│ ├── __init__.py
│ ├── test_ner_processor.py
│ ├── test1.py
│ └── test.txt
├── conftest.py
├── pytest.ini
└── run_tests.py
VS Code Configuration
The .vscode/settings.json file has been configured to:
- Set pytest as the test framework:
"python.testing.pytestEnabled": true - Point to the correct test directory:
"python.testing.pytestArgs": ["backend/tests"] - Set the working directory:
"python.testing.cwd": "${workspaceFolder}/backend" - Configure Python interpreter: Points to backend virtual environment
Running Tests
From VS Code Test Explorer
- Open the Test Explorer panel (Ctrl+Shift+P → "Python: Configure Tests")
- Select "pytest" as the test framework
- Select "backend/tests" as the test directory
- Tests should now appear in the Test Explorer
From Command Line
# From the project root
cd backend
python -m pytest tests/ -v
# Or use the test runner script
python run_tests.py
From VS Code Terminal
# Make sure you're in the backend directory
cd backend
pytest tests/ -v
Test Configuration
pytest.ini
- testpaths: Points to the
testsdirectory - python_files: Looks for files starting with
test_or ending with_test.py - python_functions: Looks for functions starting with
test_ - markers: Defines test markers for categorization
conftest.py
- Path setup: Adds backend directory to Python path
- Fixtures: Provides common test fixtures
- Environment setup: Handles test environment initialization
Troubleshooting
Tests Not Discovered
- Check VS Code settings: Ensure
python.testing.pytestArgspoints tobackend/tests - Verify working directory: Ensure
python.testing.cwdis set to${workspaceFolder}/backend - Check Python interpreter: Make sure it points to the backend virtual environment
Import Errors
- Check conftest.py: Ensures backend directory is in Python path
- Verify init.py: Tests directory should have an
__init__.pyfile - Check relative imports: Use absolute imports from the backend root
Virtual Environment Issues
- Create virtual environment:
python -m venv .venv - Activate environment:
- Windows:
.venv\Scripts\activate - Unix/MacOS:
source .venv/bin/activate
- Windows:
- Install dependencies:
pip install -r requirements.txt
Test Examples
Simple Test
def test_simple_assertion():
"""Simple test to verify pytest is working"""
assert 1 == 1
assert 2 + 2 == 4
Test with Fixture
def test_with_fixture(sample_data):
"""Test using a fixture"""
assert sample_data["name"] == "test"
assert sample_data["value"] == 42
Integration Test
def test_ner_processor():
"""Test NER processor functionality"""
from app.core.document_handlers.ner_processor import NerProcessor
processor = NerProcessor()
# Test implementation...
Best Practices
- Test naming: Use descriptive test names starting with
test_ - Test isolation: Each test should be independent
- Use fixtures: For common setup and teardown
- Add markers: Use
@pytest.mark.slowfor slow tests - Documentation: Add docstrings to explain test purpose