给我一个python+pytest+allure 打造接口自动化pom框架
时间: 2023-08-08 07:07:40 浏览: 178
基于Python的接口自动化框架
好的,下面是一个简单的 Python + Pytest + Allure 实现接口自动化 POM 框架的示例:
1. 安装依赖库
```
pip install requests
pip install pytest
pip install allure-pytest
```
2. 创建项目目录结构:
```
├── api
│ ├── __init__.py
│ ├── base_api.py
│ └── user_api.py
├── common
│ ├── __init__.py
│ ├── constants.py
│ ├── logger.py
│ └── utils.py
├── config
│ ├── __init__.py
│ └── config.py
├── data
│ ├── __init__.py
│ └── users.json
├── pytest.ini
├── README.md
├── requirements.txt
├── testcases
│ ├── __init__.py
│ ├── conftest.py
│ ├── test_login.py
│ └── test_user.py
└── pytest.ini
```
3. 编写配置文件 `config.py`、常量文件 `constants.py` 等。
`config.py`:
```python
class Config:
API_BASE_URL = 'https://xxx.com/api'
USERNAME = 'testuser'
PASSWORD = 'testpass'
```
`constants.py`:
```python
class StatusCode:
SUCCESS = 200
CLIENT_ERROR = 400
SERVER_ERROR = 500
```
4. 创建 HTTP 请求封装类 `base_api.py` 和业务接口类 `user_api.py`
`base_api.py`:
```python
import requests
from common.logger import logger
from common.constants import StatusCode
from config.config import Config
class BaseApi:
def __init__(self):
self.session = requests.session()
self.base_url = Config.API_BASE_URL
def request(self, method, url, **kwargs):
url = self.base_url + url
response = self.session.request(method, url, **kwargs)
logger.info(f'{method} {url} {kwargs} response: {response.json()}')
return response
def get(self, url, params=None, **kwargs):
return self.request('get', url, params=params, **kwargs)
def post(self, url, data=None, json=None, **kwargs):
return self.request('post', url, data=data, json=json, **kwargs)
def put(self, url, data=None, **kwargs):
return self.request('put', url, data=data, **kwargs)
def delete(self, url, **kwargs):
return self.request('delete', url, **kwargs)
def assert_status_code(self, response, expected_status_code):
assert response.status_code == expected_status_code, \
f'Expected status code is {expected_status_code}, but actual is {response.status_code}'
response_json = response.json()
assert response_json['code'] == StatusCode.SUCCESS, \
f'Response code is {response_json["code"]}, message is {response_json["message"]}'
```
`user_api.py`:
```python
from api.base_api import BaseApi
from common.constants import StatusCode
from config.config import Config
from common.utils import json_load
class UserApi(BaseApi):
def __init__(self):
super().__init__()
def login(self):
url = '/login'
data = {
'username': Config.USERNAME,
'password': Config.PASSWORD
}
response = self.post(url, json=data)
self.assert_status_code(response, StatusCode.SUCCESS)
return response.json()['data']['access_token']
def get_user_info(self, user_id):
url = f'/users/{user_id}'
headers = {
'Authorization': f'Bearer {self.login()}'
}
response = self.get(url, headers=headers)
self.assert_status_code(response, StatusCode.SUCCESS)
return response.json()['data']
def create_user(self, data):
url = '/users'
headers = {
'Authorization': f'Bearer {self.login()}'
}
response = self.post(url, json=data, headers=headers)
self.assert_status_code(response, StatusCode.SUCCESS)
return response.json()['data']
def delete_user(self, user_id):
url = f'/users/{user_id}'
headers = {
'Authorization': f'Bearer {self.login()}'
}
response = self.delete(url, headers=headers)
self.assert_status_code(response, StatusCode.SUCCESS)
return response.json()['data']
def get_random_user(self):
users = json_load('data/users.json')
return users[0]
```
5. 编写测试用例 `test_user.py` 和 `test_login.py`
`test_user.py`:
```python
import pytest
from api.user_api import UserApi
class TestUser:
@pytest.fixture(scope='class')
def user(self):
return UserApi().get_random_user()
def test_create_and_delete_user(self, user):
user_api = UserApi()
new_user = user_api.create_user(user)
assert new_user['username'] == user['username']
user_api.delete_user(new_user['id'])
def test_get_user_info(self):
user_api = UserApi()
user_info = user_api.get_user_info(1)
assert user_info['username'] == 'admin'
```
`test_login.py`:
```python
import pytest
from api.user_api import UserApi
class TestLogin:
def test_login(self):
user_api = UserApi()
access_token = user_api.login()
assert access_token is not None
```
6. 运行测试用例
在项目根目录下执行以下命令:
```
pytest --alluredir=./allure-results testcases/
```
7. 生成报告
在项目根目录下执行以下命令:
```
allure serve ./allure-results
```
此时会启动一个本地服务,打开浏览器输入 `http://localhost:port` 即可查看测试报告。
以上就是一个简单的 Python + Pytest + Allure 实现接口自动化 POM 框架的示例,希望对你有帮助。
阅读全文