根据上述要求,给个代码
时间: 2024-01-03 09:02:45 浏览: 28
很抱歉,由于代码复杂度和功能多样性,无法在此提供完整的代码。但是,我可以给您提供一些示例代码和代码片段,帮助您更好地理解如何搭建一个基于Pytest、Requests和Allure的POM模式接口自动化测试框架。
首先,下面是一个基本的测试类 `BaseApiTest`,用于封装一些公共的方法,包括发送请求、解析响应等。您可以将以下代码保存在 `base_api_test.py` 文件中:
```python
import requests
class BaseApiTest:
def __init__(self, base_url):
self.base_url = base_url
def send_request(self, method, endpoint, headers=None, params=None, data=None, json=None):
url = self.base_url + endpoint
response = requests.request(method=method, url=url, headers=headers, params=params, data=data, json=json)
return response
def get_response_json(self, response):
return response.json()
def assert_status_code(self, response, status_code):
assert response.status_code == status_code, f"Expected status code {status_code}, but got {response.status_code}"
```
接着,下面是一个 `ApiPage` 类的示例代码,用于封装一个接口的所有请求和响应信息。您可以将以下代码保存在 `api_page.py` 文件中:
```python
from .base_api_test import BaseApiTest
class ApiPage(BaseApiTest):
def __init__(self, base_url):
super().__init__(base_url)
def get_posts(self):
endpoint = "/posts"
response = self.send_request(method="GET", endpoint=endpoint)
return self.get_response_json(response)
def create_post(self, data):
endpoint = "/posts"
headers = {"Content-Type": "application/json"}
response = self.send_request(method="POST", endpoint=endpoint, headers=headers, json=data)
return self.get_response_json(response)
def update_post(self, post_id, data):
endpoint = f"/posts/{post_id}"
headers = {"Content-Type": "application/json"}
response = self.send_request(method="PUT", endpoint=endpoint, headers=headers, json=data)
return self.get_response_json(response)
def delete_post(self, post_id):
endpoint = f"/posts/{post_id}"
response = self.send_request(method="DELETE", endpoint=endpoint)
return self.get_response_json(response)
```
然后,下面是一个 `ApiTest` 类的示例代码,用于封装一个测试用例。在该类中,我们调用 `ApiPage` 类的方法来发送请求和断言响应。您可以将以下代码保存在 `test_api.py` 文件中:
```python
import allure
from .api_page import ApiPage
@allure.feature("Posts API")
class TestPostsApi:
@allure.story("Get posts")
def test_get_posts(self):
api = ApiPage(base_url="https://jsonplaceholder.typicode.com")
response = api.get_posts()
api.assert_status_code(response, 200)
@allure.story("Create a post")
def test_create_post(self):
api = ApiPage(base_url="https://jsonplaceholder.typicode.com")
data = {"userId": 1, "title": "Test Post", "body": "This is a test post."}
response = api.create_post(data)
api.assert_status_code(response, 201)
@allure.story("Update a post")
def test_update_post(self):
api = ApiPage(base_url="https://jsonplaceholder.typicode.com")
post_id = 1
data = {"title": "Updated Test Post", "body": "This is an updated test post."}
response = api.update_post(post_id, data)
api.assert_status_code(response, 200)
@allure.story("Delete a post")
def test_delete_post(self):
api = ApiPage(base_url="https://jsonplaceholder.typicode.com")
post_id = 1
response = api.delete_post(post_id)
api.assert_status_code(response, 200)
```
接着,下面是一个 `conftest.py` 文件的示例代码,用于定义全局的Pytest fixture。这里我们使用 `pytest_configure()` 和 `pytest_unconfigure()` 函数来初始化和清理日志。您可以将以下代码保存在 `conftest.py` 文件中:
```python
import logging
import pytest
@pytest.fixture(scope="session", autouse=True)
def configure_logging():
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
def pytest_configure(config):
logging.info("Initializing test suite...")
def pytest_unconfigure(config):
logging.info("Cleaning up test suite...")
```
最后,下面是一个 `pytest.ini` 文件的示例代码,用于配置Allure报告生成相关的选项。您可以将以下代码保存在 `pytest.ini` 文件中:
```
[pytest]
addopts = --alluredir=allure-results
testpaths = tests
python_files = test_*.py
python_classes = Test*
python_functions = test_*
markers =
smoke: Run the smoke tests only
regression: Run the regression tests only
api: Tests related to the API functionality
[allure]
clean_alluredir = true
disable_test_id_escaping = true
```
希望这些示例代码和代码片段对您有所帮助。如果您有其他问题或疑问,欢迎随时向我提问。