01 pytest框架简介
一、简介
pytest是一个测试用例管理框架,在unitTest基础上做的全面的升级,自由度更高,全程都是基于指令的形式来运行。
二、pytest环境部署
pip install pytest
三、用例格式
pytest框架内用例格式:测试模块必须以test开头,测试用例类必须以 Test开头,且内部函数必须以test开头
四、用例运行
①右键执行
②命令行执行
pytest test_login.py -s (pytest 默认不输出任何打印信息,如果需要看打印信息,需要添加-s 指令)
pytest test_login.py --html=report.html (--html=report.html为输出pytest原生报告)
③配置文件
pytest的配置文件通常放在测试目录下,名称为pytest.ini,命令行运行时会使用该配置文件中的配置.
[pytest] addopts = -s ... # 空格分隔,可添加多个命令行参数 -所有参数均为插件包的参数 testpaths = ./scripts # 当前目录下的scripts文件夹 -可自定义 python_files = test_*.py # 当前目录下的scripts文件夹下,以test_开头,以.py结尾的所有文件 -可自定义 python_classes = Test_* # 当前目录下的scripts文件夹下,以test_开头,以.py结尾的所有文件中,以Test_开头的类 -可自定义 python_functions = test_* # 当前目录下的scripts文件夹下,以test_开头,以.py结尾的所有文件中,以Test_开头的类内,以test_开头的方法 -可自定义
说明:pytest默认寻找当前路径下所有文件与子文件夹中以test开头的文件夹、文件、函数作为识别对象
五、参数化
@pytest.mark.parametrize('username', ['dandan','bobo']) # 装饰器内需要传可迭代对象(单个参数可以传列表,多个参数可以传元组)
def test_username(username):
assert username == 'dandan'
@pytest.mark.parametrize('x, y', [('3+5', 8), ('2+4', 6), ('6*9', 42),])
def test_eval(x, y):
assert eval(x) == y

浙公网安备 33010602011771号