给我一个python+pytest 打造接口自动化pom框架
时间: 2023-10-07 22:07:46 浏览: 64
好的,以下是一个基于Python + pytest 打造接口自动化POM框架的示例代码:
1. 安装pytest库
```bash
pip install pytest
```
2. 创建项目目录结构
```
+-- config/
| +-- __init__.py
| +-- config.py
+-- pages/
| +-- __init__.py
| +-- base_page.py
| +-- login_page.py
+-- tests/
| +-- __init__.py
| +-- test_login.py
+-- utils/
| +-- __init__.py
| +-- api.py
| +-- logger.py
| +-- parse_yaml.py
| +-- report.py
| +-- send_email.py
+-- pytest.ini
+-- requirements.txt
```
3. 编写配置文件
config/config.py
```python
import os
# 项目根目录路径
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
# 接口基础 URL
BASE_URL = "http://api.example.com"
# 测试报告目录路径
REPORT_DIR = os.path.join(ROOT_DIR, "reports")
# 日志文件目录路径
LOG_DIR = os.path.join(ROOT_DIR, "logs")
# 邮件相关配置
MAIL_HOST = "smtp.163.com"
MAIL_PORT = 465
MAIL_USER = "example@163.com"
MAIL_PASSWORD = "password"
MAIL_RECEIVER = ["example@qq.com"]
```
4. 编写基础页面类
pages/base_page.py
```python
from utils.api import send_request
class BasePage(object):
def __init__(self):
self.base_url = "http://api.example.com"
self.session = requests.session()
self.headers = {
"User-Agent": "Mozilla/5.0",
"Accept": "application/json",
"Content-Type": "application/json",
}
def send_request(self, method, url, params=None, data=None, json=None, **kwargs):
url = self.base_url + url
res = send_request(self.session, method, url, params=params, data=data, json=json, headers=self.headers, **kwargs)
return res
```
5. 编写登录页面类
pages/login_page.py
```python
from pages.base_page import BasePage
class LoginPage(BasePage):
def __init__(self):
super(LoginPage, self).__init__()
self.url = "/login"
def login(self, username, password):
data = {"username": username, "password": password}
res = self.send_request("post", self.url, json=data)
return res
```
6. 编写测试用例
tests/test_login.py
```python
import pytest
from pages.login_page import LoginPage
from utils.parse_yaml import parse_yaml
data = parse_yaml("data/login_data.yaml")
class TestLogin(object):
@pytest.fixture(scope="class", autouse=True)
def setup_class(self):
self.login_page = LoginPage()
@pytest.mark.parametrize("test_data", data)
def test_login(self, test_data):
res = self.login_page.login(test_data["username"], test_data["password"])
assert res["status"] == test_data["expected"]
```
7. 编写pytest配置文件
pytest.ini
```ini
[pytest]
log_cli = true
log_cli_level = INFO
log_cli_format = %(asctime)s - %(levelname)s - %(message)s
log_cli_date_format = %Y-%m-%d %H:%M:%S
log_file = logs/pytest.log
log_file_level = INFO
log_file_format = %(asctime)s - %(levelname)s - %(message)s
log_file_date_format = %Y-%m-%d %H:%M:%S
addopts = -s -v --html=reports/pytest_report.html --self-contained-html
```
8. 编写运行脚本
run.py
```python
import os
import pytest
from config.config import REPORT_DIR, LOG_DIR, MAIL_HOST, MAIL_PORT, MAIL_USER, MAIL_PASSWORD, MAIL_RECEIVER
from utils.logger import Logger
from utils.report import generate_report
from utils.send_email import send_mail
if __name__ == "__main__":
# 创建日志文件夹
if not os.path.exists(LOG_DIR):
os.makedirs(LOG_DIR)
# 创建测试报告文件夹
if not os.path.exists(REPORT_DIR):
os.makedirs(REPORT_DIR)
# 设置日志器
logger = Logger().get_logger()
# 运行pytest测试用例
pytest.main(["-s", "-v", "--alluredir", "allure_report"])
# 生成测试报告
generate_report(REPORT_DIR)
# 发送邮件
send_mail(MAIL_HOST, MAIL_PORT, MAIL_USER, MAIL_PASSWORD, MAIL_RECEIVER, REPORT_DIR)
```
9. 编写运行脚本所需的依赖
requirements.txt
```
pytest
allure-pytest
pytest-html
pytest-rerunfailures
pytest-xdist
requests
PyYAML
allure-python-commons
```
10. 运行测试用例
在命令行中进入项目根目录,执行以下命令:
```bash
python run.py
```
以上是一个基于Python + pytest 打造接口自动化POM框架的示例代码,供你参考。