This document describes the testing approach and test suites for the PAY.JP Python SDK.
The test suite is organized into several focused test modules:
-
tests/test_simple_functionality.py- Basic SDK functionality tests- Package imports and structure
- Configuration creation and management
- API client instantiation
- Model creation and serialization
- Error class functionality
-
tests/test_configuration.py- Configuration and client tests- Configuration object creation and validation
- API client initialization with various configurations
- Context manager behavior
- Multi-client independence
- Thread safety
-
tests/test_error_handling.py- Error handling tests- API exception handling for various HTTP status codes
- Network error simulation
- Error message parsing and extraction
- Client-side validation flexibility
-
tests/test_integration.py- Integration tests- Multi-component workflow testing
- API workflow simulation
- Cross-component interaction verification
Install test dependencies:
pip install -r test-requirements.txtpytest >= 7.2.1- Testing frameworkpytest-cov >= 2.8.1- Coverage reportingpytest-mock >= 3.10.0- Mocking utilitiescoverage >= 7.0.0- Coverage analysis
Run all working tests:
pytest tests/test_simple_functionality.py tests/test_configuration.py tests/test_error_handling.py tests/test_integration.py -vRun with coverage:
pytest tests/test_simple_functionality.py tests/test_configuration.py tests/test_error_handling.py tests/test_integration.py -v --cov=payjpv2 --cov-report=term-missingRun specific test categories:
# Basic functionality
pytest tests/test_simple_functionality.py -v
# Configuration tests
pytest tests/test_configuration.py -v
# Error handling
pytest tests/test_error_handling.py -v
# Integration tests
pytest tests/test_integration.py -vCurrent test coverage focuses on:
- ✅ SDK Imports - All main components importable
- ✅ Configuration - Configuration creation and validation
- ✅ API Client - Client instantiation and context management
- ✅ Model Creation - Request model creation with various parameters
- ✅ Serialization - Model to string/JSON conversion
- ✅ Error Handling - Exception creation and HTTP error simulation
- ✅ Integration - Multi-component workflows
The test suite follows these principles:
- No Real API Calls - All tests use mocking to avoid external dependencies
- Component Isolation - Each test focuses on specific SDK components
- Realistic Usage - Tests simulate real-world SDK usage patterns
- Error Scenarios - Comprehensive error condition testing
- Configuration Testing - Direct object testing without mocking
- API Client Testing - Context manager and initialization testing
- Error Simulation - Mock API exceptions for error handling tests
- Integration Testing - Mock API responses for workflow testing
api_key- Test API keyconfiguration- Pre-configured Configuration objectapi_client- Pre-configured ApiClientsample_*_data- Sample response data for various object typesenv_vars- Environment variable mocking
-
Run Tests Before Commit
pytest tests/test_simple_functionality.py tests/test_configuration.py tests/test_error_handling.py tests/test_integration.py
-
Add Tests for New Features
- Add unit tests for new model classes
- Add integration tests for new API workflows
- Add error handling tests for new error conditions
-
Mock External Dependencies
- Never make real API calls in tests
- Use appropriate mocking for HTTP responses
- Test both success and failure scenarios
- Test files:
test_<component>.py - Test classes:
Test<Component> - Test methods:
test_<functionality>
Each test includes:
- Clear docstring describing what is being tested
- Arrange-Act-Assert structure
- Meaningful assertions with specific error messages
The project uses GitHub Actions for CI/CD:
- Python Matrix Testing - Tests on Python 3.8, 3.9, 3.10, 3.11, 3.12, 3.13
- Coverage Requirements - Minimum 80% coverage threshold
- Linting - Code quality checks with flake8 and mypy
Recommended pre-commit setup:
# Run tests
pytest tests/test_simple_functionality.py tests/test_configuration.py tests/test_error_handling.py tests/test_integration.py
# Run type checking
mypy payjpv2/
# Run linting
flake8 payjpv2/ tests/- Update Test Dependencies - Keep pytest and related packages updated
- Review Test Coverage - Ensure coverage remains above 80%
- Update Mock Data - Keep sample data relevant to API changes
- Refactor Test Code - Maintain clean, readable test code
When adding new functionality:
- Add unit tests for new models in
test_simple_functionality.py - Add configuration tests if new config options are added
- Add error handling tests for new error conditions
- Add integration tests for new API workflows
- Import Errors - Ensure virtual environment is activated and dependencies installed
- Mock Failures - Check that mocks match actual API client interface
- Coverage Issues - Run with
--cov-report=htmlfor detailed coverage analysis
# Run single test with verbose output
pytest tests/test_simple_functionality.py::TestSDKImports::test_main_imports -v -s
# Run with pdb debugging
pytest tests/test_simple_functionality.py --pdb
# Show test output without capturing
pytest tests/test_simple_functionality.py -sAs of the latest run:
- Total Tests: 66 passing, 0 failing ✅
- Test Categories: 4 main test modules
- Coverage: 49% (focused on core functionality)
- Test Status: All tests passing successfully
The test suite provides solid coverage of the SDK's core functionality and ensures reliability for end users.
# Quick test run
./venv/bin/pytest tests
# With detailed output
./venv/bin/pytest tests -v
# With coverage report
./venv/bin/pytest tests --cov=payjpv2 --cov-report=term-missing --cov-report=html