Python automated scripts for database testing
This project demonstrates how to use SQLAlchemy to interact with a MySQL database and how to write automated tests using pytest. It includes examples for schema validation, query performance analysis and data consistency checks.
Table of Contents
- Installation
- Project Structure
- Database Configuration
- Database connection configuration
- Running the Tests
To get started, clone the repository and install the necessary dependencies:
git clone https://github.com/LaunchCG/python-SQLAlchemyAccelerator.git
cd your-repo-name
pip install -r requirements.txt
Make sure to have the following packages in your requirements.txt:
SQLAlchemy
mysqlclient
pytest
pandas
pytest-html
The project is organized as shown above:
src/
│
├── database/
│ ├── db.py # Database configuration and connection
│
├── tests/
│ ├── conftest.py # Pytest configuration (fixtures)
│ ├── test_schema.py # Tests for schema validation
│ ├── test_performance.py # Tests for query performance
│ ├── test_consistency.py # Tests for data consistency
│
├── .env # Environment variables for sensitive information.
├── pytest.ini # Configuration file for pytest
├── requirements.txt # List of dependencies
└── README.md # Project documentation
The database connection is configured in the src/database/db.py file. Update the environment variables in the .env file with your MySQL credentials as shown above.
DATABASE_USER = os.getenv('DATABASE_USER', 'root')
DATABASE_PASSWORD = os.getenv('DATABASE_PASSWORD', 'root')
DATABASE_HOST = os.getenv('DATABASE_HOST', 'localhost')
DATABASE_NAME = os.getenv('DATABASE_NAME', 'pytest_workshop')
To run the tests, simply execute:
pytest