Building a Python API Automation Testing Framework: Design, Configuration, Logging, Database, and Email Integration
This article details the step‑by‑step creation of a Python‑based API automation testing framework, covering project structure, configuration files, a reusable logging module, HTTP request handling with requests, common utilities for Excel and XML, MySQL database access, email reporting, and the test runner setup.
After the company's testing focus shifted from web UI to API testing, the author decided to build a Python automation framework to replace manual Postman/JMeter work, noting Python's simplicity compared to Java.
The framework starts with a config.ini file that stores immutable settings such as database credentials, HTTP endpoints, and email parameters, allowing easy updates without changing code. readConfig.py reads these settings using configparser and provides getter methods ( get_email, get_http, get_db) for other modules.
The Log.py module creates a thread‑safe logger: it builds a timestamped result directory, configures a logging.FileHandler with a formatter, and exposes the logger via a singleton MyLog class. ConfigHttp.py wraps the requests library, exposing set_url, set_headers, set_params, set_data, and set_files methods, and provides get and post functions that handle timeouts and logging.
The common package includes utilities for reading test cases from Excel ( xlrd) and SQL statements from XML, as well as the logging class used throughout the framework.
Database interaction is encapsulated in MyDB, which reads connection parameters from config.ini, establishes a MySQL connection via pymysql, executes SQL, fetches results, and closes the connection.
Email reporting is implemented in configEmail.py. It builds a multipart message, attaches a zipped test report, and sends it through a 163.com SMTP server, with thread‑safe singleton access via MyEmail.
The test runner ( runAll.py) reads a caselist.txt file, loads test cases with unittest, runs them using HTMLTestRunner to generate an HTML report, and optionally sends the report via email based on the on_off flag.
Directory layout: result stores timestamped logs and reports; testCase holds unittest scripts prefixed with test; testFile contains the Excel case files and XML SQL files; caselist.txt lists the cases to execute.
Overall, the article provides a complete, modular Python framework for API testing, demonstrating how to separate configuration, logging, HTTP handling, data management, and reporting to create a maintainable automated test suite.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
