Pytest优点认知:
可以结合所有的自动化测试工具跳过失败用例以及失败重跑结合allure生产美观报告和Jenkins持续集成很多强大的插件
pytest-html:生产html测试报告
pytest-xdist:多线程运行
pytest-ordering:改变用例执行顺序
pytest-rerunfailures:失败用例重爬
allure-pytest:美观测试报告
一般项目中,会使用requerments.text文档保存插件名称,进行批量一次性安装
运行方式:
主函数运行方式:main方法运行命令运行方式pytest-vs-v:更加详细信息-s:调试信息-n=处理:多线程运行--reruns=数字:失败用例重跑--reruns=数字:失败用例重跑--html=./report.html:生成html报告
用例分组运行
进行用例分组:用例进行注解:
#@pytest.mark.分组名称 如下:
前置后置,夹具
简单区分:直接调用方法,但是接口过多时,比较麻烦
def setup(self):
print('每个用例执行之前,都执行一遍')
def teardown(self):
print('每个用例执行之后,都执行一遍')
实现部分前置:如只想之一个用例进行前置,如登录时需要连接数据库。
需要使用装置器:
参数介绍:
@pytest.fixture(scope='作用域',params='数据驱动',autouse='是否自动执行',ids='自定义参数',name='重命名')作用域:可以函数、类、模块、包、session
使用方法:
需要前置的功能函数上进行标注装置器别的方法函数之间调用装置器
如下:一个文件里面进行部分前置唤醒
import time
import pytest
import requests
#实现装置器标注前置,进行标注,yieid进行唤醒返回
@pytest.fixture(scope='function')
def conn_getbase():
print('连接数据库成功')
yield
print('关闭数据库成功')
class TestSendRequsets:
#过多接口时,比较麻烦冗余
# def setup(self):
# print('每个用例执行之前')
#
# def teardown(self):
# print('每个用例执行之后')
def test_getImgCode(self):
# 接口url
t = time.time()
timess = str(int(round(t * 1000)))
times = str(int(t))
url = 'http://124.71.230.185:9002/jeecg-boot/sys/randomImage/' + '' + timess
# 参数
data = {
'_t': times,
}
# # get请求
rep = requests.request('get', url, params=data)
print(rep.text)
# 标注为smoke分组用例
@pytest.mark.smoke
def test_Login(self,conn_getbase):
# post请求
url = 'http://124.71.230.185:9002/jeecg-boot/sys/login'
# 参数
data = {
'captcha': 'Gkak!@#2021',
'checkKey': 1637811815838,
'password': '123456',
'remember_me': 1,
'username': 'admin'
}
rep = requests.request('post', url, json=data)
statues = rep.json()['success']
message = rep.json()['message']
if statues:
print('')
else:
# raise Exception(message)
print(message)
if __name__ == '__main__':
pytest.main();
封装灵活调用
一般情况下:@pytest.fixture()会和conftest.py文件一块使用
conftest.py名称是固定的,功能如下:
用处是多个py文件之间共享前置配置。里面的方法在调用时,不需要导入,可以之间调用可以都多个conftest.py文件,也可以有不同的层级
conftest.py文件详情请看下一章
实现:
# 前置函数
import pytest
@pytest.fixture(scope='function')
def conn_getbase():
print('连接数据库成功')
yield
print('关闭数据库成功')
文章为作者独立观点,不代表股票交易接口观点