0
0
Testing Fundamentalstesting~8 mins

Stored procedure testing in Testing Fundamentals - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Stored procedure testing
Folder Structure
stored-procedure-testing/
├── tests/
│   ├── test_procedures.sql       -- SQL scripts to test stored procedures
│   ├── test_data.sql             -- Setup test data
│   └── cleanup.sql               -- Cleanup test data
├── procedures/
│   └── my_procedure.sql          -- Stored procedure source code
├── config/
│   └── db_config.json            -- Database connection settings
├── utils/
│   └── db_helper.py              -- Helper scripts for DB connection and query execution
└── reports/
    └── test_report.html          -- Test execution reports
  
Test Framework Layers
  • Database Layer: Contains stored procedures and test data scripts.
  • Test Scripts Layer: SQL scripts that call stored procedures with test cases and verify outputs.
  • Utility Layer: Helper scripts (e.g., Python) to connect to the database, run SQL scripts, and fetch results.
  • Configuration Layer: Holds database connection details and environment settings.
  • Reporting Layer: Generates readable test reports from test execution results.
Configuration Patterns
  • Use a db_config.json file to store database host, port, username, password, and database name.
  • Support multiple environments (dev, test, prod) by having separate config sections or files.
  • Use environment variables or secure vaults to keep credentials safe and avoid hardcoding.
  • Allow test runner scripts to read config dynamically to connect to the correct database.
Test Reporting and CI/CD Integration
  • Generate HTML or XML reports summarizing which stored procedure tests passed or failed.
  • Include details like input parameters, expected vs actual results, and error messages.
  • Integrate test execution into CI/CD pipelines to run tests automatically on code changes.
  • Fail the build if critical stored procedure tests fail to prevent faulty database code deployment.
Best Practices
  • Isolate test data: Prepare and clean up test data before and after each test to avoid side effects.
  • Use assertions on stored procedure outputs to verify correctness.
  • Test edge cases and error handling inside stored procedures.
  • Keep test scripts simple and readable, focusing on one scenario per test.
  • Automate tests to run frequently and catch regressions early.
Self Check

Where in this folder structure would you add a new test script to verify a recently added stored procedure?

Key Result
Organize stored procedure tests with clear layers: SQL test scripts, utilities for DB access, config for environments, and reporting for results.