DocumentationTesting
Testing
A beginner's guide to testing in Pynions using pytest.
Testing with pytest
Quick Start
- Make sure you're in your virtual environment:
source venv/bin/activate
- Run all tests:
pytest
- Run tests with output:
pytest -v # verbose output
pytest -s # show print statements
pytest -v -s # both verbose and print statements
Environment Setup
- Install test dependencies:
pip install -e .
pip install pytest pytest-asyncio pytest-cov
- Create basic test structure:
mkdir -p tests/test_plugins
touch tests/__init__.py tests/test_plugins/__init__.py
Basic Test Structure
Create test files in the tests/
directory:
# tests/test_example.py
def test_basic_example():
result = 1 + 1
assert result == 2
Testing Async Functions
For testing async functions (like our Serper plugin):
import pytest
@pytest.mark.asyncio
async def test_async_function():
result = await some_async_function()
assert result is not None
Test Configuration
Create pytest.ini
in your project root:
[pytest]
python_files = test_*.py
python_classes = Test*
python_functions = test_*
testpaths = tests
addopts = -v -s --tb=short
asyncio_mode = auto
Common Testing Patterns
1. Setup and Teardown
Use fixtures for common setup:
import pytest
@pytest.fixture
def sample_data():
"""Provide sample data for tests"""
return {
"query": "test query",
"max_results": 10
}
def test_with_fixture(sample_data):
assert sample_data["query"] == "test query"
2. Testing Exceptions
import pytest
def test_error_handling():
with pytest.raises(ValueError):
# This should raise ValueError
raise ValueError("Expected error")
3. Parametrized Tests
import pytest
@pytest.mark.parametrize("input,expected", [
("test", "TEST"),
("hello", "HELLO"),
])
def test_uppercase(input, expected):
assert input.upper() == expected
Running Specific Tests
# Run single test file
pytest tests/test_example.py
# Run specific test function
pytest tests/test_example.py::test_basic_example
# Run tests matching pattern
pytest -k "test_basic"
Test Coverage
- Run tests with coverage:
pytest --cov=pynions
# With detailed report
pytest --cov=pynions --cov-report=html
- View coverage report:
open htmlcov/index.html
Best Practices
-
Test File Organization
- Keep tests in
tests/
directory - Match source file structure
- Use clear test names
- Keep tests in
-
Test Naming
- Prefix test files with
test_
- Use descriptive test function names
- Example:
test_serper_search_returns_results
- Prefix test files with
-
Assertions
- Use specific assertions
- Test one thing per test
- Include error messages
-
Environment
- Use fixtures for setup
- Clean up after tests
- Don't modify production data
Debugging Tests
- Show print output:
pytest -s
- Enable debug logging:
import logging
logging.basicConfig(level=logging.DEBUG)
- Use breakpoints:
def test_debug_example():
breakpoint() # Starts debugger here
assert True
Common Issues
-
Module Not Found
- Check virtual environment
- Install package in editable mode:
pip install -e .
-
Async Test Failures
- Use
@pytest.mark.asyncio
- Configure
asyncio_mode = auto
- Use
-
Fixture Errors
- Check fixture scope
- Verify fixture dependencies
Need help? Check the debugging guide: markdown:docs/debugging.md
Unit Tests
- Test successful responses
- Test error handling
- Test rate limiting
- Test invalid API keys
See the Serper plugin documentation for specific examples:
Updated 3 months ago
Edit this page