product_data.json
run.py
import pytest
import osif __name__ == '__main__':pytest.main()os.system("allure generate temp -o reports --clean") # 生成报告并且覆盖掉之前的报告
pytest.py
[pytest]
addopts=-v -s --setup-show "./test_cases/test_addproduct.py" --alluredir=temp --clean-alluredirtestpaths=./test_cases
conftest.py
import time
import pytest
import requests@pytest.fixture()
def sccess_token():url = "http://192.168.116.128:8080/admin/login"payload = {"password": "macro123","username": "admin"}response = requests.post(url=url, json=payload).json()return response["data"]["token"]@pytest.fixture(scope="function" , autouse=True)
def fix():start_time=time.time()yieldend_time=time.time()return f"测试用例的执行时间:{end_time - start_time}"
test_cases/test_addproduct.py
import pytest
import requests
import jsonwith open(r"D:\Python\pycharm\Flask_Test\mall\data\product_data.json", mode="rt",encoding="utf8") as x: # 打开json文件result = json.load(x) # 把json文件转化成对应python格式@pytest.fixture(scope="session",params=result)
def dat(request):date = request.paramreturn datedef test_add(sccess_token,fix,dat):url = "http://192.168.3.129:端口/product/create"headers = {'Authorization': f'Bearer {sccess_token}','Content-Type': 'application/json'}response = requests.post(url=url, headers=headers, json=dat[0])# =================断言========================assert response.status_code == dat[1]["code"], "用例测试不通过"