-
Notifications
You must be signed in to change notification settings - Fork 0
Examples
prasad-kumkar edited this page May 5, 2025
·
1 revision
This guide covers testing practices and methodologies used in Tessera development.
The test suite is organized into three main categories:
tests/
├── fixtures/ # Shared test fixtures and utilities
├── integration/ # End-to-end and integration tests
└── unit/ # Unit tests for individual components
Tessera uses these testing tools:
- pytest: Main testing framework
- pytest-cov: Code coverage reporting
- pytest-asyncio: Async test support
- hypothesis: Property-based testing
Basic Usage:
# Run all tests
pytest
# Run with verbose output
pytest -v
# Run specific test file
pytest tests/unit/test_state.py
# Run specific test function
pytest tests/unit/test_state.py::test_state_transitionCoverage Reports:
# Generate coverage report
pytest --cov=jam
# Generate HTML report
pytest --cov=jam --cov-report=html
# View report in browser
open htmlcov/index.html- Unit Tests:
# tests/unit/test_feature.py
import pytest
from jam.feature import Feature
def test_feature_behavior():
# Arrange
feature = Feature()
# Act
result = feature.process()
# Assert
assert result == expected- Integration Tests:
# tests/integration/test_workflow.py
import pytest
from jam.workflow import Workflow
@pytest.mark.asyncio
async def test_end_to_end():
# Setup
workflow = Workflow()
# Execute
result = await workflow.run()
# Verify
assert result.status == "success"- Create Fixtures:
# tests/fixtures/conftest.py
import pytest
@pytest.fixture
def test_data():
return {
"key": "value"
}
@pytest.fixture
async def async_resource():
resource = await setup_resource()
yield resource
await cleanup_resource()- Use Fixtures:
# tests/unit/test_feature.py
def test_with_fixture(test_data):
assert process(test_data) == expected
@pytest.mark.asyncio
async def test_async(async_resource):
result = await async_resource.query()
assert resultUsing Hypothesis:
from hypothesis import given, strategies as st
@given(st.integers(), st.integers())
def test_addition_properties(x, y):
result = add(x, y)
assert result == add(y, x) # Commutative
assert add(result, 0) == result # IdentityUsing pytest-mock:
def test_with_mock(mocker):
# Create mock
mock_db = mocker.patch('jam.database.Database')
mock_db.query.return_value = expected_result
# Use mock
result = process_data()
# Verify
mock_db.query.assert_called_once()
assert result == expected_result-
Test Organization:
- One test file per module
- Clear test names describing behavior
- Group related tests in classes
-
Test Design:
- Follow Arrange-Act-Assert pattern
- Test edge cases and error conditions
- Keep tests focused and isolated
-
Fixtures:
- Use fixtures for common setup
- Clean up resources properly
- Share fixtures via conftest.py
-
Coverage:
- Aim for high test coverage
- Focus on critical paths
- Don't sacrifice quality for coverage
-
Performance:
- Keep tests fast
- Use async testing where appropriate
- Minimize external dependencies