信计2001 翟佳树
-- coding: utf-8 --
import requests
import allure
import json
class TestPet:
@staticmethod
def setup_class():
初始化测试数据
global test_pet_id
test_pet_id = 0
@staticmethod
def teardown_class():
#删除测试数据
url = f"https://petstore.swagger.io/v2/pet/{test_pet_id}"
res = requests.delete(url)
assert res.status_code == 200
@allure.step(“新增宠物”)
def test_add_pet(self):
url = “https://petstore.swagger.io/v2/pet”
headers = {‘Content-Type’: ‘application/json’}
data = {“id”: 0000, “category”:
{“id”: 0000, “name”: “string”}, “name”: “testpet”, “photoUrls”:
[“string”], “tags”:
[{“id”: 0, “name”: “string”}], “status”: “available”}
res = requests.post(url, headers=headers,
data=json.dumps(data))
assert res.status_code == 200 or res.status_code == 201,
f"新增宠物接口异常,状态码为{res.status_code}"
global test_pet_id
test_pet_id =
res.json().get(‘id’) # 记录新增宠物id
allure.attach(str(data), “请求参数”)
allure.attach(str(res.json()), “响应结果”)
@allure.step(“查询宠物”)
def test_query_pet(self):
url = f"https://petstore.swagger.io/v2/pet/findByStatus?status=available"
res = requests.get(url)
assert res.status_code == 200, f"查询宠物接口异常,状态码为{res.status_code}"
allure.attach(str(res.json()), “查询返回结果”)
接口测试
数据2102崔鑫通
接口测试
信计2002刘冬冬
import requests
import pytest
import allure
PET_BASE_URL = 'https://example.com/pet'
FIND_BY_STATUS_URL = PET_BASE_URL + '/findByStatus'
@pytest.fixture
def pet_data():
return {
"name": "cute dog",
"photoUrls": ["https://example.com/photo/1", "https://example.com/photo/2"],
"status": "available"
}
@allure.step('发送新增宠物请求')
def create_pet(pet_data):
response = requests.post(PET_BASE_URL, json=pet_data)
return response
@allure.step('发送查询宠物请求')
def find_pet(status='available'):
params = {'status': status}
response = requests.get(FIND_BY_STATUS_URL, params=params)
return response
def test_create_pet(pet_data):
response = create_pet(pet_data)
assert response.status_code == 200
assert response.json()['name'] == pet_data['name']
def test_find_pet():
response = find_pet()
assert response.status_code == 200
@allure.feature('宠物管理')
@allure.story('新增宠物')
def test_create_pet_with_allure(pet_data):
"""测试新增宠物接口,使用Allure报告记录测试步骤"""
with allure.step('发送新增宠物请求'):
response = create_pet(pet_data)
with allure.step('断言状态码为200'):
assert response.status_code == 200
with allure.step('断言返回结果中的name字段与请求中的相同'):
assert response.json()['name'] == pet_data['name']
@allure.feature('宠物管理')
@allure.story('查询宠物')
def test_find_pet_with_allure():
"""测试查询宠物接口,使用Allure报告记录测试步骤"""
with allure.step('发送查询宠物请求'):
response = find_pet()
with allure.step('断言状态码为200'):
assert response.status_code == 200
信计2002刘冬冬
数据2102王平安
数据2101江博伦
数据2101袁冬颖aaahttps://gitee.com/yuandongying/yuandongying1.git