DanielMevs / Pytest-Practice---Real-World-Testing-Examples

Practical pytest examples demonstrating testing best practices for CSV validation, database operations, and API mocking

Repository from Github https://github.comDanielMevs/Pytest-Practice---Real-World-Testing-ExamplesRepository from Github https://github.comDanielMevs/Pytest-Practice---Real-World-Testing-Examples

Pytest Practice - Real-World Testing Examples

A collection of practical pytest examples demonstrating testing best practices for data validation, database operations, and API interactions.

🎯 Overview

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.

πŸ“ Repository Structure

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

πŸ§ͺ Testing Examples

1. CSV Data Validation (csv_handling.py)

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_columns

Skills Demonstrated: βœ… Data validation testing
βœ… File handling in tests
βœ… Fixture usage for test data
βœ… pandas integration

2. Database Testing (database_handling.py)

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

3. API Testing with Mocking (test_user_profile.py)

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

πŸ› οΈ Technologies Used

  • 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)

πŸš€ Running the Tests

Prerequisites

pip install pytest pandas requests

Execute Tests

# 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

Expected Output

============================= 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 ==============================

πŸ’‘ Testing Patterns Demonstrated

pytest Fixtures

@pytest.fixture()
def db_connection():
    conn = sqlite3.connect('Chinook_Sqlite.sqlite')
    yield conn
    conn.close()

Parametrized Testing

@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")
])

Exception Testing

def test_user_not_found_exception(mock_get):
    with pytest.raises(ValueError, match="User 1 not found"):
        get_user_profile(1)

Mock Verification

mock_get.assert_called_once_with("expected_url")

🎯 Real-World Applications

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

πŸ“š Learning Objectives

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

πŸ“Š Sample Data Sources

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.

πŸ”— Additional Resources


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.

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Practical pytest examples demonstrating testing best practices for CSV validation, database operations, and API mocking

License:MIT License


Languages

Language:Python 100.0%