pytest环境准备01

pytest是python的一种单元测试框架,与python自带的unittest测试框架类似,但是比unittest框架使用起来更简洁,效率更高。根据pytest的官方网站介绍,它具有如下特点:

  • 非常容易上手,入门简单,文档丰富,文档中有很多实例可以参考
  • 能够支持简单的单元测试和复杂的功能测试
  • 支持参数化
  • 执行测试过程中可以将某些测试跳过(skip),或者对某些预期失败的case标记成失败
  • 支持重复执行(rerun)失败的case
  • 支持运行由nose, unittest编写的测试case
  • 可生成html报告
  • 方便的和持续集成工具jenkins集成
  • 可支持执行部分用例
  • 具有很多第三方插件,并且可以自定义扩展

common_function.py

def login(s):
‘‘‘登录获取token‘‘‘
# s = requests.session() # 会话 代码里面的浏览器,模拟浏览器的功能
url = "api/Account/PwdLogin?"
par = {
‘mobile‘: 1730963,
‘password‘: 156

}

h = {‘appkey‘: ‘454‘}

# 转json
r = requests.get(url=url, headers=h, params=par)
print(r.json())

# token
token = r.json()[‘response‘][‘token‘]
print(f"取出token:{token}")

h = {
"Authorization": f"Bearer {token}",
‘appkey‘: ‘44‘
}
s.headers.update(h) # 更新到session会话
# 更新之后的头部
print(s.headers)
return token


if __name__ == ‘__main__‘:
s = requests.session()
login(s)

 

conftest.py

import pytest
import requests
from case.common_function import login


@pytest.fixture(scope="module")
def login_fix():
‘‘‘自定义一个前置的操作‘‘‘
print("先登陆")
s = requests.session()
login(s)
return s


@pytest.fixture(scope="function")
def unlogin_fix():
‘‘‘自定义一个前置的操作‘‘‘
print("不登陆")
s = requests.session()
s.headers.update({"Authorization": "Token f4b9a1dffbf525ecc93f8c80035c60fa546d5xxx"})
return s


test_detail.py
import allure
import pytest

test_data = [
[44763, {"message": "获取成功"}],
[44990, {"message": "商品不存在或已删除"}],
[447, {"message": "商品不存在或已删除"}],
]

@allure.story("登录用例")
@pytest.mark.parametrize("test_input,expect", test_data)
def test_info_34(login_fix, test_input, expect):
s = login_fix
url = "/api/ProductSpu/BusinessDetail?"
par = {
‘spuId‘: test_input
}
r = s.get(url, params=par)
print(r.json())

assert r.json()[‘msg‘] == expect[‘message‘]

 

pytest --alluredir ./report/allure_raw

allure serve report/allure_raw

pytest

pytest环境准备01

上一篇:vue生命周期 created和mounted的区别


下一篇:3. 无重复字符的最长子串