ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

pytes--配置文件pytest.ini

2021-09-14 11:34:34  阅读:164  来源: 互联网

标签:__ 配置文件 -- mark pytest ini test def


前言

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

ini配置文件

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

  pytest.ini pytest的主配置文件,可以改变pytest的默认行为

  conftest.py 测试用例的一些fixture配置

  __init__.py 识别该文件为python的package包

  tox.ini 与 pytest.ini类似,用tox工具时候才有用

  setup.cfg 也是ini格式文件,影响setup.py的行为

ini文件基本格式

#保存为pytest.ini文件
[pytest]
 
addopts = -rsxX
xfail_strict = true<br>

-rsxX表示pytest报告所有测试用例被跳过、预计失败、预计失败但实际被通过的原因。

使用pytest --help指令可以查看pytest.ini的设置选项

img

mark标记

如下案例,使用2个标签:test和add(名字可以随便起),使用mark标记功能对于以后分类测试非常有用处;

首先当前包下,新建pytest.ini文件,写入以下内容(注意:若是pytest.ini中不添加mark的标签,可以执行成功,但会报警告):

img

标记好之后,pytest0729的目录下使用pytest --markers查看(注意非当前pytest.ini目录,执行该命令,不会显示标记):

img

参考代码:

#test_answers.py
import pytest
@pytest.mark.test
def test_send_http():
    print("mark web test")
 
def test_something_quick():
    pass
 
def test_another():
    pass
 
@pytest.mark.add
class TestClass:
    def test_01(self):
        print("hello :")
 
    def test_02(self):
        print("hello world!")
 
if __name__ == "__main__":
    pytest.main(["-v", "test_answers.py", "-m=add"])

cmd下运行 pytest -v -m=add

img

禁用xpass

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

#test_answers.py
import pytest
def test_xixi():
    print("xixi")
    assert 1
@pytest.mark.xfail()
def test_t1():
    a="hello"
    b="hello sky"
    assert a==b
 
@pytest.mark.xfail()
def test_t2():
    a="hello"
    b="hello sky"
    assert a!=b
if __name__=="__main__":
    pytest.main(["-s","test_answers.py"]) 

img

img

标签:__,配置文件,--,mark,pytest,ini,test,def
来源: https://www.cnblogs.com/ye-peng/p/15266711.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有