Testing Strategy & Automation
Relevant source files
The following files were used as context for generating this wiki page:
Testing Strategy & Automation in the faceit-python library ensures the reliability, type safety, and API compatibility of the FACEIT API wrapper. The project employs a multi-layered testing approach, ranging from unit tests of internal utilities to end-to-end (e2e) tests against the live production API. This strategy validates both synchronous and asynchronous implementations of the library's resources.
The automation suite leverages pytest as the primary test runner, supplemented by pytest-asyncio for handling asynchronous flows. Static analysis tools like mypy and ruff are integrated into the development workflow to enforce strict typing and code quality standards before tests are executed.
Sources: pyproject.toml:50-60, tests/test_e2e_data_resource.py:18-24
Testing Infrastructure and Tooling
The library uses a modern Python testing stack configured via pyproject.toml. This configuration defines dependency groups, linting rules, and test markers to streamline the automation process.
Core Testing Tools
| Tool | Purpose | Configuration Detail |
|---|---|---|
| pytest | Primary test framework | Version >= 9.0.0 |
| pytest-asyncio | Async test support | asyncio_mode = "auto" |
| mypy | Static type checking | Strict mode enabled; excludes docs/tests |
| ruff | Linting and formatting | Targets Python 3.10; includes isort and bugbear |
| httpx | HTTP mocking | Used with unittest.mock for integration tests |
Sources: pyproject.toml:52-100
Test Markers and Environments
The suite identifies specific tests using markers. The e2e marker distinguishes tests that require a network connection and a valid FACEIT_API_KEY. These tests are conditionally skipped if the python-decouple library is missing or if the environment variable is not set.
Sources: pyproject.toml:98-100, tests/test_e2e_data_resource.py:18-24
Testing Layers
The project organizes tests into three distinct layers to ensure comprehensive coverage.
This diagram shows the hierarchy of testing layers, moving from isolated logic to full system integration.
Unit Testing
Unit tests focus on isolated components without external dependencies.
- Custom Types: Validates Pydantic-based types like
FaceitID,FaceitTeamID, andFaceitMatchIDfor correct UUID formatting and prefixing. - Pagination Logic: Tests
BasePageIterator,SyncPageIterator, andAsyncPageIteratorusing dummy resources to verify item gathering and deduplication. - HTTP Client: Validates
BaseAPIClientbehavior, including API key masking, header generation, retry logic, and SSL error handling.
Sources: tests/test_custom_types.py:1-100, tests/test_pagination.py:15-100, tests/test_http_client.py:100-250
Integration Testing
Integration tests verify the interaction between library modules and mocked external services. They ensure that DataResource correctly routes requests to the appropriate endpoints with expected parameters.
This diagram illustrates the flow of an integration test where the network layer is replaced with mocks to verify internal request construction.
Sources: tests/test_resources_integration.py:15-60, tests/test_data_resource_integration.py:50-80
End-to-End (E2E) Testing
E2E tests verify the library's functionality against the live FACEIT API. These tests validate:
- Authentication: Successful initialization with
FACEIT_API_KEY. - Data Integrity: Mapping of real API responses to Pydantic models (e.g.,
Player,CS2MatchPlayerStats). - Pagination Loops: The ability to fetch multiple pages of real data using
all_itemsorall_matches_stats.
Sources: tests/test_e2e_data_resource.py:30-80
Mocking Strategy
The project extensively uses unittest.mock to isolate components during non-E2E tests.
HTTP Mocking
Tests for SyncDataResource and AsyncDataResource patch httpx.Client and httpx.AsyncClient respectively. This allows developers to:
- Verify the final URL constructed by the resource.
- Inspect the query parameters (e.g.,
offset,limit). - Simulate various API responses, including
200 OK,400 Bad Request, and500 Server Error.
Sources: tests/test_resources_integration.py:15-40, tests/test_http_client.py:20-60
Pagination Mocking
Pagination tests often mock the SyncPageIterator.unix or gather_from_iterator methods to ensure that high-level convenience methods (like all_history) correctly delegate to the underlying iterator logic without performing actual network calls.
Sources: tests/test_players_resource.py:63-80
Automated Quality Assurance
Automation is enforced through linting and type checking configurations.
- Strict Typing:
mypyis configured withstrict = true, ensuring all code (excluding tests and docs) is fully typed. - Linting Rules:
ruffenforces rules such asisortfor import organization andflake8-bugbearfor finding likely bugs. It specifically ignores certain errors in test files (e.g.,S101for asserts) to allow standard testing practices. - Async Safety:
ruffmonitors forASYNC119(yielding in async generators) and other async-specific pitfalls.
Sources: pyproject.toml:62-150
Conclusion
The faceit-python testing strategy provides a robust framework for maintaining a type-safe API wrapper. By combining strict static analysis with a tiered approach to dynamic testing—unit, integration, and e2e—the project ensures that changes to the library or the underlying FACEIT API are quickly identified and resolved. The use of markers and environment-based skipping allows for flexible execution across different development environments and CI/CD pipelines.