dev #2
|
|
@ -0,0 +1,118 @@
|
||||||
|
# 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
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
# App package
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
# Core package
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
# Document handlers package
|
||||||
|
|
@ -0,0 +1,33 @@
|
||||||
|
import pytest
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
# Add the backend directory to Python path for imports
|
||||||
|
backend_dir = Path(__file__).parent
|
||||||
|
sys.path.insert(0, str(backend_dir))
|
||||||
|
|
||||||
|
# Also add the current directory to ensure imports work
|
||||||
|
current_dir = Path(__file__).parent
|
||||||
|
sys.path.insert(0, str(current_dir))
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def sample_data():
|
||||||
|
"""Sample data fixture for testing"""
|
||||||
|
return {
|
||||||
|
"name": "test",
|
||||||
|
"value": 42,
|
||||||
|
"items": [1, 2, 3]
|
||||||
|
}
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def test_files_dir():
|
||||||
|
"""Fixture to get the test files directory"""
|
||||||
|
return Path(__file__).parent / "tests"
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def setup_test_environment():
|
||||||
|
"""Setup test environment before each test"""
|
||||||
|
# Add any test environment setup here
|
||||||
|
yield
|
||||||
|
# Add any cleanup here
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
[tool:pytest]
|
||||||
|
testpaths = tests
|
||||||
|
pythonpath = .
|
||||||
|
python_files = test_*.py *_test.py
|
||||||
|
python_classes = Test*
|
||||||
|
python_functions = test_*
|
||||||
|
addopts =
|
||||||
|
-v
|
||||||
|
--tb=short
|
||||||
|
--strict-markers
|
||||||
|
--disable-warnings
|
||||||
|
markers =
|
||||||
|
slow: marks tests as slow (deselect with '-m "not slow"')
|
||||||
|
integration: marks tests as integration tests
|
||||||
|
unit: marks tests as unit tests
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Simple test runner script to verify test discovery and execution
|
||||||
|
"""
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
def run_tests():
|
||||||
|
"""Run pytest with proper configuration"""
|
||||||
|
# Change to backend directory
|
||||||
|
backend_dir = Path(__file__).parent
|
||||||
|
os.chdir(backend_dir)
|
||||||
|
|
||||||
|
# Run pytest
|
||||||
|
cmd = [sys.executable, "-m", "pytest", "tests/", "-v", "--tb=short"]
|
||||||
|
|
||||||
|
print(f"Running tests from: {backend_dir}")
|
||||||
|
print(f"Command: {' '.join(cmd)}")
|
||||||
|
print("-" * 50)
|
||||||
|
|
||||||
|
try:
|
||||||
|
result = subprocess.run(cmd, capture_output=False, text=True)
|
||||||
|
return result.returncode
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error running tests: {e}")
|
||||||
|
return 1
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
exit_code = run_tests()
|
||||||
|
sys.exit(exit_code)
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
# Tests package
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
def test_basic_discovery():
|
||||||
|
"""Basic test to verify pytest discovery is working"""
|
||||||
|
assert True
|
||||||
|
|
||||||
|
def test_import_works():
|
||||||
|
"""Test that we can import from the app module"""
|
||||||
|
try:
|
||||||
|
from app.core.document_handlers.ner_processor import NerProcessor
|
||||||
|
assert NerProcessor is not None
|
||||||
|
except ImportError as e:
|
||||||
|
pytest.fail(f"Failed to import NerProcessor: {e}")
|
||||||
|
|
||||||
|
def test_simple_math():
|
||||||
|
"""Simple math test"""
|
||||||
|
assert 1 + 1 == 2
|
||||||
|
assert 2 * 3 == 6
|
||||||
Loading…
Reference in New Issue