legal-doc-masker/backend/docs/TEST_SETUP.md

119 lines
3.4 KiB
Markdown

# 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:
1. **Set pytest as the test framework**: `"python.testing.pytestEnabled": true`
2. **Point to the correct test directory**: `"python.testing.pytestArgs": ["backend/tests"]`
3. **Set the working directory**: `"python.testing.cwd": "${workspaceFolder}/backend"`
4. **Configure Python interpreter**: Points to backend virtual environment
## Running Tests
### From VS Code Test Explorer
1. Open the Test Explorer panel (Ctrl+Shift+P → "Python: Configure Tests")
2. Select "pytest" as the test framework
3. Select "backend/tests" as the test directory
4. Tests should now appear in the Test Explorer
### From Command Line
```bash
# 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
```bash
# Make sure you're in the backend directory
cd backend
pytest tests/ -v
```
## Test Configuration
### pytest.ini
- **testpaths**: Points to the `tests` directory
- **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
1. **Check VS Code settings**: Ensure `python.testing.pytestArgs` points to `backend/tests`
2. **Verify working directory**: Ensure `python.testing.cwd` is set to `${workspaceFolder}/backend`
3. **Check Python interpreter**: Make sure it points to the backend virtual environment
### Import Errors
1. **Check conftest.py**: Ensures backend directory is in Python path
2. **Verify __init__.py**: Tests directory should have an `__init__.py` file
3. **Check relative imports**: Use absolute imports from the backend root
### Virtual Environment Issues
1. **Create virtual environment**: `python -m venv .venv`
2. **Activate environment**:
- Windows: `.venv\Scripts\activate`
- Unix/MacOS: `source .venv/bin/activate`
3. **Install dependencies**: `pip install -r requirements.txt`
## Test Examples
### Simple Test
```python
def test_simple_assertion():
"""Simple test to verify pytest is working"""
assert 1 == 1
assert 2 + 2 == 4
```
### Test with Fixture
```python
def test_with_fixture(sample_data):
"""Test using a fixture"""
assert sample_data["name"] == "test"
assert sample_data["value"] == 42
```
### Integration Test
```python
def test_ner_processor():
"""Test NER processor functionality"""
from app.core.document_handlers.ner_processor import NerProcessor
processor = NerProcessor()
# Test implementation...
```
## Best Practices
1. **Test naming**: Use descriptive test names starting with `test_`
2. **Test isolation**: Each test should be independent
3. **Use fixtures**: For common setup and teardown
4. **Add markers**: Use `@pytest.mark.slow` for slow tests
5. **Documentation**: Add docstrings to explain test purpose