1.pytest里的配置文件
pytest.ini: pytest的主配置文件,可以改变pytest的默认行为
conftest.py: 测试用例的一些fixture配置
setup.cfg: 也是ini格式文件,影响setup.py的行为
tox.ini: tox.ini与pytest.ini类似,用tox工具时候才有用
2. pytest.ini
存放位置: 项目文件夹下
注意点:
所有的运行命令都要遵从pytest.ini文件
2.1范例
[pytest]
# 参数
addopts= -vs
testpaths= ./test_cases
# 重新规定用例所在文件 类 方法名称规则
python_files= test_c*.py
python_classes=
python_functions=
# 可以通过命令pytest --markers查看
markers=
smoking: smoking test cases
high: high test cases
项目文件夹下建立run.py文件
import pytest
import os
import time
# 主运行函数,运行规则遵照pytest.ini文件
if __name__ == '__main__':
pytest.main()
time.sleep(3)
# todo:运行生成报告命令
# 将 ./allure-results 目录下的测试数据生成html测试报告到 ./fileresult 路径下。–clean 先清空测试报告目录,再生成新的测试报告。
os.system("allure generate ./result -o ./report --clean")
time.sleep(3)
#启动一个web服务,将已经生成的html测试报告在默认的浏览器中打开,地址为:http://localhost:885/
os.system("allure open -h 127.0.0.1 -p 885 ./report")
2.2帮助文档
使用pytest --help指令可以查看pytest.ini的设置选项
[pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg file found:
markers (linelist) markers for test functions
empty_parameter_set_mark (string) default marker for empty parametersets
norecursedirs (args) directory patterns to avoid for recursion
testpaths (args) directories to search for tests when no files or dire
console_output_style (string) console output: classic or with additional progr
usefixtures (args) list of default fixtures to be used with this project
python_files (args) glob-style file patterns for Python test module disco
python_classes (args) prefixes or glob names for Python test class discover
python_functions (args) prefixes or glob names for Python test function and m
xfail_strict (bool) default for the strict parameter of
addopts (args) extra command line options
minversion (string) minimally required pytest version
2.3配置项说明
1.markers
有时候标签多了,不容易记住,为了方便后续执行指令的时候能准确使用mark的标签,可以写入到pytest.ini文件
# pytest.ini
[pytest]
markers =
webtest: Run the webtest case
hello: Run the hello case
也可以使用命令 pytest --markers查看pytest.ini里markers的信息
2.xfail
xfail标记用例为失败(场景:如果依赖的用例都失败,那么这个用例也没有意义了,所以标记为失败)
有两种标记方式:
- 案例中: pytest.xfail(“登录不成功, 标记为xfail”) https://www.cnblogs.com/yoyoketang/p/9525602.html
- 案例上 :@pytest.mark.xfail()
https://www.cnblogs.com/yoyoketang/p/9550648.html
虽然用例被标记了失败,但是可能实际案例断言都是正确的(案例通过),那么案例在测试结果里的状态就是不一样的
collecting ... collected 3 items
test_xpass.py::test_hello PASSED [ 33%]
test_xpass.py::test_yoyo1 xfail [ 66%] #标记xfail,但是实际案例测试不通过
test_xpass.py::test_yoyo2 XPASS [100%] #标记xfail,但是实际案例测试通过
=============== 1 passed, 1 xfailed, 1 xpassed in 0.27 seconds ================
此时设置xfail_strict = true可以让那些标记为@pytest.mark.xfail但实际通过的测试用例被报告为失败
# pytest.ini
[pytest]
markers =
webtest: Run the webtest case
hello: Run the hello case
xfail_strict = true
测试结果就变为
collecting ... collected 3 items
test_xpass.py::test_hello PASSED [ 33%]
test_xpass.py::test_yoyo1 xfail [ 66%]
test_xpass.py::test_yoyo2 FAILED [100%]
================================== FAILURES ===================================
_________________________________ test_yoyo2 __________________________________
[XPASS(strict)]
================ 1 failed, 1 passed, 1 xfailed in 0.05 seconds ================
3.addopts
addopts参数可以更改默认命令行选项,这个当我们在cmd输入指令去执行用例的时候,会用到,比如我想测试完生成报告,指令比较长
$ pytest -v --reruns 1 --html=report.html --self-contained-html
每次输入这么多,不太好记住,于是可以加到pytest.ini里
# pytest.ini
[pytest]
markers =
webtest: Run the webtest case
hello: Run the hello case
xfail_strict = true
addopts = -v --reruns 1 --html=report.html --self-contained-html