by 西西
安装:pip install -U pytest
Pycharm pytest 配置
三种运行方式:
Pycharm 界面运行
右键文件/目录运行
使用命令行方式运行
pytest --help 帮助文档,查看所有命令行参数
pytest -k “add” 匹配所有名称中包含add的用例(‘add or div’ ‘TestClass’)(录播)
pytest -x 遇到失败的用例就停止执行
pytest -m mark标签名 标记
pytest --collect-only 只收集用例
pytest --junitxml=./result.xml 生成执行结果文件
类似的 setup,teardown 同样更灵活
模块级: setup_module/teardown_module 模块始末,全局的(优先最高)
函数级: setup_function/teardown_function 只对函数用例生效(不在类中)
类级: setup_class/teardown_class 只在类中前后运行一次(在类中)
方法级: setup_method/teardown_methond 开始于方法始末(在类中)
方法级: setup/teardown 运行在调用方法的前后
待测试的输入和输出是一组数据, 可以把测试数据组织起来,用不同的测试数据调用相同的测试方法
数据驱动就是数据的改变从而驱动自动化测试的执行,最终引起测试结果的改变。
https://www.runoob.com/w3cnote/yaml-intro.html
[pytest]
markers 自定义mark 标签名
addopts 运行时参数(可添加多个命令行参数,空格分隔,所有参数与命令行一致)
python_files 自定义测试文件命名规则
python_classes = Test_* 自定义测试类命名规则
python_functions= test_* check_* 自定义测试方法命名规则
norecursedirs = result logs datas test_demo* 运行时忽略某些文件夹
[pytest]
;日志开关 true false
log_cli = true
;日志级别
log_cli_level = info
;打印详细日志,相当于命令行加 -vs
addopts = --capture=no
;日志格式
log_cli_format = %(asctime)s [%(levelname)s] %(message)s (%(filename)s:%(lineno)s)
;日志时间格式
log_cli_date_format = %Y-%m-%d %H:%M:%S
注意: windows系统,pytest.ini 文件不能使用中文注释