【pytest-16】配置文件pytest.ini

前言

pytest配置文件可以改变pytest的运行方式,它是一个固定的pytest.ini文件,读取配置信息。

ini配置文件

  1. pytest.ini是主配置文件
  2. conftest测试用例的一些fixture配置
  3. _init__.py识别该文件夹为python的package包
  4. setup.cfg也是ini格式文件,影响setup.py的行为

pytest.ini放置路径

放置于项目的根目录,命名规范,不要乱起名字,一般情况下,一个工程下一个pytest.ini文件。

 基本格式

 查看选项具体信息

执行pytest --help查看pytest.ini的设置

pytest --help

 其他常用配置项

  • mark标记

#encoding:utf-8
#@Time:2020/12/9 11:27
#@Author:sunny

import pytest

@pytest.mark.vx
def test_vx():
    print("测试微信")

def test_douyin():
    pass


def test_noMark():
    print("没有标记的测试")

@pytest.mark.toutiao
class TestClass:
    def test_method(self):
        print("测试头条")
    def test_method2(self):
        print("测试头条2")

if __name__ == '__main__':
    pytest.main(["-v","test_mark.py","-m=toutiao"])

用pytest -v -m toutiao执行

运行结果:

 有时候标签太多的话,不方便记忆,直接写入到pytest.ini文件

  标记好之后,可以通过pytest -markers查询到

pytest --markers

 红色圈住的两个就是写入到pytest.ini的配置了

  • xpassed

设置xfail_strict=true,可以让那些标记为@pytest.mark.xfail但实际结果通过显示为Xpassed的测试用例被报告为失败。

格式:True、False(默认),1、0

#encoding:utf-8
#@Time:2020/12/14 14:24
#@Author:sunny

import pytest

def test_hello():
    print("hello world")

@pytest.mark.xfail()
def test_1():
    a="hello"
    b="hello world"
    assert a==b

@pytest.mark.xfail()
def test_2():
    a="hello"
    b="hello world"
    assert a !=b

if __name__ == '__main__':
    pytest.main(["-s","test_xpass.py"])

运行结果:

 由运行结果可以看出,test_1和test_2用例,两个都被我们标记失败了,我们希望不被运行直接标记为失败显示xfail,实际test_2却通过并显示了xpassed(X),为了让两个都显示xfail,那就加个配置xfail_strict = true,强制标记。

 再次运行,结果就变成

 这样标记为xpass的就变成了failed

  • addopts

可以更改默认命令行选项,这个当我们在cmd输入指令去执行用例的时候,可以用参数代替,省的每次都需要手动输入

示例:失败重跑1次,并且将结果记录到测试报告

 pytest -v --reruns 1 --html=report.html --self-contained-html

  • --reruns 1  失败重跑1次
  • –html=report.html 指定测试报告名称
  • –self-contained-html 在html页面本地加载css样式

 下次我直接输入pytest,就默认带上这些参数了。

 

posted @ 2020-12-14 15:54  做一只热爱生活的小透明  阅读(162)  评论(0)    收藏  举报