A comprehensive testing framework built with Python and Pytest for API testing in huge scale.
This project provides a robust framework for testing REST. It follows Python best practices and Pytest conventions.
- API Testing: Client abstraction for REST API endpoints (GET, POST, PUT, DELETE)
- Test Fixtures: Reusable Pytest fixtures for API client
- Type Hints: Full type annotations for better IDE support and code clarity
- Error Handling: Comprehensive error handling and logging throughout the framework
API-BATCH-TEST/
│ ├── config/ # Configuration files
│ ├── src/
│ │ ├── helpers/ # Helper functions for tests
│ │ ├── schemas/ # JSON schemas for response validation
│ │ ├── api_client.py # REST API client abstraction
│ │ └── api_endpoints.py # API endpoint definitions
│ │
│ ├── tests/
│ │ ├── cassettes/ # VCR.py cassettes for recorded HTTP interactions
│ │ ├── data/ # Test data files
│ │ ├── test-contract/ # Contract tests
│ │ ├── test-negative/ # Negative tests
│ │ ├── test-pagination/ # Pagination tests
│ │ ├── test-smoke/ # Smoke tests
│ │ └── conftest.py # Pytest fixtures and configuration
│ │
│ ├── pyproject.toml # Project metadata and dependencies
│ ├── pyvenv.cfg # Virtual environment configuration
│ └── requirements.txt # Python dependencies
│
├── reports/ # Test reports
├── mypy.ini # Mypy configuration
├── pytest.ini # Pytest configuration
└── README.md
Provides a simple abstraction for HTTP operations:
APIClient.get()- Retrieve dataAPIClient.post()- Create resourcesAPIClient.put()- Update resourcesAPIClient.delete()- Remove resources
Supports URL path parameters and query strings.
Reusable test fixtures:
__all__ = ["base_url", "http"]- Expose fixtures for test modules
### Cloning the repository
git clone https://github.com/AlexAlexandreAlves/api-batch-test.git
### Run through the venv (virtual environment), for this, you need to create and activate it first
python3 -m venv {$name}
source {$name}/bin/activate
### Copy all folders and files in the main project and past into your recent created env-folder, and then install the dependencies:
cd {your-venv-folder}
pip install -r requirements.txt
Key Dependencies:
pytest- Testing frameworkrequests- HTTP client librarypsycopg2-binary- PostgreSQL adapter for Pythonmypy- Static type checkerpytest-reporter-html1- HTML test report generationpytest-xdist- Parallel test executionpytest-sugar- Enhanced test outputjsonschema- JSON schema validationpython-dotenv- Environment variable managementvcrpy- HTTP interaction recording
# Run all tests
pytest
# Run tests with @pytest.mark (example: smoke)
pytest -m smoke
# Run with verbose output
pytest -v
# Run with print statements visible
pytest -s -v
# Run specific test file
pytest tests/test_smoke/test_sample_smoke.py -v
# Generate HTML report
pytest --html=reports/html1-report.htmldef test_api_available(http, base_url):
r = http.get(Endpoints.PEOPLE)
assert r.status_code == 200
assert r.headers.get("Content-Type", "").startswith("application/json")- Fixtures: Leverage Pytest fixtures for setup and teardown
- Type Hints: Use type annotations for better code documentation
- Error Handling: Catch and log errors appropriately
- Test Isolation: Each test should be independent and repeatable
- Async Support: For I/O-bound tests, consider using
pytest-asyncio
See requirements.txt for the complete list. Key dependencies:
| Package | Version | Purpose |
|---|---|---|
| pytest | Latest | Testing framework |
| requests | Latest | HTTP client |
| psycopg2-binary | Latest | PostgreSQL adapter |
| mypy | Latest | Type checking |
| pytest-reporter-html1 | Latest | HTML reports |
| pytest-xdist | Latest | Parallel test execution |
| pytest-sugar | Latest | Enhanced output |
| jsonschema | Latest | JSON schema validation |
| python-dotenv | Latest | Environment variables |
- Activate virtual environment:
source venv/bin/activate - Reinstall dependencies:
pip install -r requirements.txt
- Use
-vflag for verbose output - Use
-sflag to see print statements
This project is open source and available under the MIT License.
AlexAlexandreAlves
Happy Testing! 🧪