Pytest学习(十四)- 配置文件pytest.ini的使用

pytest.ini的作用

可以改变pytest的运行方式,读取配置信息,并按指定的方式去运行

非test文件

pytest里面有些文件是非test文件

  • pytest.ini:pytest的主配置文件,可以改变pytest的默认行为
  • conftest.py:测试用例的一些fixture配置
  • init.py:识别该文件夹为python的package包

pytest.ini应该放哪里?

必须放在项目根目录下 ,不要乱放、乱起其他名字

配置的使用

marks的使用

作用:测试用例中添加了 @pytest.mark.webtest 装饰器,如果不添加marks选项的话,就会报warnings
格式:list列表类型
示例:

[pytest]
markers =
    mylogin: this is mylogin page
    query: this is query page
    addcart: this is addcart page

关于案例,请移步到《Pytest学习(八) - 自定义标记mark的使用》

xfail_strict的使用

作用:设置xfail_strict = True可以让那些标记为@pytest.mark.xfail但实际通过显示XPASS的测试用例被报告为失败
格式:True 、False(默认),1、0
示例:

[pytest]
markers =
    mylogin: this is mylogin page
    query: this is query page
    addcart: this is addcart page
xfail_strict = True

案例代码如下:

# -*- coding: utf-8 -*-
# @Time    : 2020/11/29 11:01
# @Author  : longrong.lang
# @FileName: test_pytestini.py
# @Software: PyCharm
# @Cnblogs :https://www.cnblogs.com/longronglang
import pytest


@pytest.mark.xfail()
def test_case():
    assert 1 == 1

1、未设置 xfail_strict = True 时,测试结果显示XPASS
执行效果如下:

2、已设置 xfail_strict = True 时,测试结果显示FAILED
执行效果如下:

addopts的使用

1、安装插件

pip3 install pytest-html -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
pip3 install pytest-rerun -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
pip3 install pytest-count -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

2、作用

addopts参数可以更改默认命令行选项,这个当我们在cmd输入指令去执行用例的时候,会用到,比如我想测试完生成报告,指令比较长

pytest -v —rerun 1 —html=report.html —self-contained-html

每次输入这么多,不太好记住,于是可以加到pytest.ini里,示例代码如下:

[pytest]
markers =
    mylogin: this is mylogin page
    query: this is query page
    addcart: this is addcart page
xfail_strict = True

addopts = -v --rerun 1 --html=report.html --self-contained-html

这样下次打开cmd,直接输入pytest,它就能默认带上这些参数了

log_cli的使用

作用:控制台实时输出日志
格式:log_cli=True 或False(默认),或者log_cli=1 或 0

log_cli=0的运行结果

log_cli=1的运行结果

结论
很明显,加了log_cli=1之后,可以清晰看到哪个package下的哪个module下的哪个测试用例是否passed还是failed;

这个地方我还是没太弄明白,怎么用,有懂得盆友可以在评论下方留言,还请赐教。

posted @ 2020-11-29 13:19  久曲健  阅读(892)  评论(0编辑  收藏  举报