A collection of practical pytest examples demonstrating testing best practices for data validation, database operations, and API interactions.
This repository showcases pytest testing techniques through realistic scenarios commonly encountered in QA automation and data engineering roles. Each example demonstrates different testing patterns and pytest features.
pytest-practice/
βββ csv_handling/
β βββ csv_handling.py # CSV data validation testing
β βββ organizations-100.csv # Sample CSV data for testing
βββ database_handling/
β βββ database_handling.py # Database operations and SQL testing
β βββ Chinook_Sqlite.sqlite # Sample SQLite database
βββ mock_examples/
β βββ test_user_profile.py # API testing with mocking
βββ README.md # This file
βββ LICENSE # MIT License
Scenario: Validate CSV file structure for data pipeline ingestion
Key Features:
- pandas DataFrame validation
- Column header verification
- pytest fixtures for expected data
- Real file testing
def test_columns_have_right_field(data_columns):
df = pd.read_csv('organizations-100.csv')
assert list(df.columns) == data_columnsSkills Demonstrated:
β
Data validation testing
β
File handling in tests
β
Fixture usage for test data
β
pandas integration
Scenario: Test database aggregation queries and business logic
Key Features:
- SQLite database testing
- Complex SQL query validation
- Database connection management
- Multiple test scenarios
- Edge case handling
def get_top_customers_by_avg_purchases(connection, limit=5):
# Complex aggregation query testing
query = '''SELECT CustomerId, AVG(daily_count) AS avg_purchases_per_day
FROM (SELECT CustomerId, InvoiceDate, COUNT(*) AS daily_count
FROM Invoice GROUP BY CustomerId, InvoiceDate)
GROUP BY CustomerId ORDER BY avg_purchases_per_day DESC LIMIT ?'''
return connection.execute(query, (limit,)).fetchall()Skills Demonstrated:
β
Database integration testing
β
SQL query validation
β
Connection fixture management
β
Parametrized testing
β
Edge case coverage
Scenario: Test API client with comprehensive error handling
Key Features:
- requests library mocking
- Multiple response scenarios
- Exception testing
- Parametrized test cases
- Mock verification
@patch('requests.get')
def test_user_successfully_gets_profile(mock_get, get_successful_user_data):
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.return_value = get_successful_user_data
mock_get.return_value = mock_response
result = get_user_profile(1)
assert result == get_successful_user_data
mock_get.assert_called_once_with("https://jsonplaceholder.typicode.com/users/1")Skills Demonstrated:
β
API testing with mocking
β
External dependency isolation
β
Exception handling validation
β
Mock assertion verification
β
Parametrized edge cases
- Python 3.x - Core programming language
- pytest - Testing framework with fixtures and parametrization
- pandas - Data manipulation and CSV handling
- sqlite3 - Database operations and testing
- unittest.mock - Mocking external dependencies
- requests - HTTP client library (mocked in tests)
pip install pytest pandas requests# Run all tests with verbose output
pytest -v
# Run specific test modules
pytest csv_handling/csv_handling.py -v
pytest database_handling/database_handling.py -v
pytest mock_examples/test_user_profile.py -v
# Run tests by category
pytest csv_handling/ -v # All CSV-related tests
pytest database_handling/ -v # All database tests
pytest mock_examples/ -v # All mocking examples
# Run with coverage (if pytest-cov installed)
pytest --cov=. --cov-report=html============================= test session starts ==============================
collected 7 items
csv_handling.py::test_columns_have_right_field PASSED [ 14%]
database_handling.py::test_get_top_customers PASSED [ 28%]
database_handling.py::test_top_customers_different_limits PASSED [ 42%]
database_handling.py::test_empty_result_handling PASSED [ 57%]
test_user_profile.py::test_user_successfully_gets_profile PASSED [ 71%]
test_user_profile.py::test_user_not_found_exception PASSED [ 85%]
test_user_profile.py::test_api_server_error PASSED [100%]
============================== 7 passed in 0.89s ==============================
@pytest.fixture()
def db_connection():
conn = sqlite3.connect('Chinook_Sqlite.sqlite')
yield conn
conn.close()@pytest.mark.parametrize("user_id", [
pytest.param("abc", id="non-numerical input"),
pytest.param(10000, id="invalid user id"),
pytest.param(-1, id="negative user id")
])def test_user_not_found_exception(mock_get):
with pytest.raises(ValueError, match="User 1 not found"):
get_user_profile(1)mock_get.assert_called_once_with("expected_url")These testing patterns apply to common QA automation scenarios:
Data Pipeline Testing:
- CSV/JSON validation before ingestion
- Data transformation verification
- Schema compliance checking
Database Testing:
- Query result validation
- Data integrity verification
- Performance testing foundations
API Integration Testing:
- Service dependency isolation
- Error handling validation
- Contract testing foundations
This repository demonstrates practical application of:
- Test-driven development practices
- External dependency mocking strategies
- Database testing methodologies
- Data validation techniques
- Error handling verification
- Test fixture management
- Parametrized test design
This repository uses educational datasets for realistic testing scenarios:
-
Chinook Database: SQLite sample database from lerocha/chinook-database
- Contains music store data (customers, invoices, tracks)
- Widely used for database learning and testing examples
-
Organizations CSV: Sample organizational data for CSV validation testing
- Used to demonstrate data pipeline validation techniques
Note: All sample data is included for educational purposes and complete working examples.
Note: This repository contains practical examples for learning pytest testing patterns. The sample data and database are included for educational purposes and demonstrate real-world testing scenarios commonly encountered in quality assurance and data engineering roles.
This project is licensed under the MIT License - see the LICENSE file for details.