接口测试自动化脚本框架5

 product_data.json

run.py

import pytest
import os

if __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-alluredir

testpaths=./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()

    yield

    end_time=time.time()

    return f"测试用例的执行时间:{end_time - start_time}"

test_cases/test_addproduct.py

import pytest
import requests
import json


with 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.param

  return date


def 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"], "用例测试不通过"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值