How to Build a Flexible Core Executor for API Automation Testing with pytest and Allure
This article explains how to design and implement a high‑performance, configurable core executor for API automation testing, covering dependency installation, executor class creation, logging integration, parallel execution, report generation with Allure, and practical code examples for Python developers.
The core executor is the heart of an API automation testing framework, responsible for coordinating test execution, collecting results, and generating reports; encapsulating it efficiently improves testing speed, flexibility, and maintainability.
1. Design Goals
Efficient execution (including parallel and asynchronous runs), flexible configuration (test selection, environment settings), comprehensive result collection (pass, fail, skip), multi‑format report generation (HTML, XML), and detailed logging for debugging.
2. Implementation
(a) Install required libraries:
pip install pytest allure-pytest(b) Create executor.py and define the TestExecutor class:
import pytest
import os
from loguru import logger
class TestExecutor:
def __init__(self, test_dir, report_dir="reports", log_dir="logs"):
self.test_dir = test_dir
self.report_dir = report_dir
self.log_dir = log_dir
self.logger = logger
self.logger.add(f"{log_dir}/{{time}}.log", rotation="1 day", compression="zip")
def run_tests(self, test_pattern="test_*.py", markers=None, parallel=False):
"""Run test cases.
:param test_pattern: pattern for test files
:param markers: pytest markers
:param parallel: enable parallel execution"""
pytest_args = [
self.test_dir,
f"--alluredir={self.report_dir}",
"--log-cli-level=INFO",
"--log-file-level=DEBUG",
"--log-format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'",
"--log-date-format='%Y-%m-%d %H:%M:%S'",
]
if markers:
pytest_args.append(f"-m {markers}")
if parallel:
pytest_args.append("-n auto") # pytest‑xdist for parallel runs
try:
self.logger.info("开始运行测试用例")
pytest.main(pytest_args)
self.logger.info("测试用例运行完成")
except Exception as e:
self.logger.error(f"运行测试用例失败:{e}")
raise
def generate_report(self):
"""Generate test report."""
try:
self.logger.info("开始生成测试报告")
os.system(f"allure generate {self.report_dir} -o {self.report_dir}/html --clean")
self.logger.info("测试报告生成完成")
except Exception as e:
self.logger.error(f"生成测试报告失败:{e}")
raise(c) Use the executor in run_tests.py :
from executor import TestExecutor
if __name__ == "__main__":
executor = TestExecutor(test_dir="tests", report_dir="reports", log_dir="logs")
executor.run_tests(parallel=True)
executor.generate_report()(d) Example: run tests with a specific marker (e.g., smoke )
# tests/test_smoke.py
import pytest
@pytest.mark.smoke
def test_login():
assert True
@pytest.mark.smoke
def test_logout():
assert True
from executor import TestExecutor
if __name__ == "__main__":
executor = TestExecutor(test_dir="tests", report_dir="reports", log_dir="logs")
executor.run_tests(markers="smoke", parallel=True)
executor.generate_report()(e) Parallel execution support – install pytest‑xdist :
pip install pytest-xdistRun with parallel=True as shown above.
3. Logging
Install Loguru:
pip install loguruIntegrate the logger in the executor (see class definition above) and use it in test cases:
from loguru import logger
def test_example():
logger.info("开始测试")
assert True
logger.info("测试完成")4. Report Generation
Install Allure adapter:
pip install allure-pytestThe generate_report method (shown earlier) creates an HTML report; view it with:
allure serve reports/html5. Summary
By encapsulating a core executor, developers obtain an efficient, flexible, and maintainable solution for API automation testing that supports configurable test selection, environment settings, result collection, parallel execution, detailed logging, and multi‑format report generation.
Test Development Learning Exchange
Test Development Learning Exchange
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.