美文网首页
pytest常用配置文件之pytest.ini

pytest常用配置文件之pytest.ini

作者: C1R2 | 来源:发表于2022-10-25 21:29 被阅读0次

    pytest.ini:pytest的主配置文件,可以改变pytest的默认行为,有很多可配置的选项。
    conftest.py:是本地的插件库,其中的hook函数和fixture将作用于该文件所在的目录以及所有子目录。
    init.py:每个测试子目录都包含该文件时,那么在多个测试目录中可以出现同名测试文件。

    常用配置:

    1、addopts

    作用:addopts参数可以更改默认命令行选项,当我们在cmd或Terminal输入一堆指令去执行用例的时候,就可以用该参数代替了,省去重复性的敲命令工作。命令行只需要输入pytest即可

    --reruns=1:用例失败重跑一次

    -p no:warnings, :不在结果中输出用例的告警信息

    2、配置项testpaths

    testpaths指示pytest去哪里访问。testpaths是相对于根目录的路径,限定测试用例的搜索范围。只有在命令行执行pytest未指定文件目录参数或测试用例标识符时,该选项才生效。

    3、xfail_strict = true

    由@pytest.mark.xfail装饰,但实际通过的测试用例结果为xpassed的用例被报告为失败,如下test_5所示:

    @pytest.mark.run(order=5)
    @pytest.mark.xfail(condition=True,reason='test_s预期失败结果也失败,就是预期失败成功了,结果xfailed')
    def test_2(self):
        print("*****test_2******")
        assert 2==1
    
    @pytest.mark.xfail(condition=True,reason='test_s预期失败却通过,就是预期失败失败了xpassed')
    def test_5(self):
        print("*****test_5******")
        assert 2==2
    
    4、log_cli 是否开启打印日志

    格式:log_cli=True 或False(默认),或者log_cli=1 或 0 ;log_cli=1 可以看到哪个package下的哪个module下的哪个测试用例是否passed还是failed;

    log_cli = 1
    log_cli_level = INFO
    log_cli_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)
    log_cli_date_format=%Y-%m-%d %H:%M:%S
    

    ************************************pytest 知识点汇总************************************

    单元测试框unittest,pytest
    unittest是官方的,不需要单独安装,pytest是在unittest上扩展的,是需要安装的
    pip install pytest

    如何批量运行测试用例
    如何断言
    怎样生成测试报告(可以是html,或aller集成)
    基于上面的3个问题,引入了单元测试框架unittest、pytest

    一、pytest特点如下(比unittest更简洁更高效):

    1、测试文件必须“test_”开头或“test”结尾
    2、测试方法必须以“test
    ”开头
    3、测试类命名以"Test"开头

    区别:
    unittest必须写到类里,类继承unittest.testcases,pytest的类直接Test开头即可,还可以直接写函数

    二、fixture函数

    1、setup_function() 函数执行前的操作
    2、setup_module() 模块之前,python文件之前
    3、setup_method() 在方法之前执行
    4、setup_class() 在每个类之前执行

    如果是unittest的话,需要装饰器 如下所示:

    class Test(unittest.TestCase):
        @classmethod
        def setUpClass(cls):
            print("start!")
    #初始化环境 开始执行脚本
        @classmethod
        def tearDownClass(cls):
            time.sleep(1)
            print("end!")
    #结束
        def test01(self):
            print(u"执行测试用例01")
    #开始执行测试用例...
        def test03(self):
           print(u"执行测试用例03")
    
        def test02(self):
            print(u"执行测试用例02")
    
        def addtest(self):
            print(u"add方法")
    
    if __name__ == "__main__":
      unittest.main()
    
    三、命令行参数

    pytest -sv test_study.py -s会把print打印出来,v打印的更详细

    四、失败重试

    pip install pytest-rerunfailures
    命令行加参数即可 --reruns 3 (重试3次)

    pytest -sv test_study.py --reruns 2    断言失败重试2次
    
    五、跳过测试函数

    加装饰器
    @pytest.mark.skipif(condition,reason=None) 有条件跳过
    @pytest.mark.skip 无条件跳过

    六、预期失败

    @pytest.mark.xfail(condition,reason=None)
    2 passed, 1 skipped, 1 xpassed, 1 xfailed, 1 warning in 0.04s 运行结果中的xpassed是预期失败却断言成功了; xfailed预期失败却断言失败了,

    七、调整测试用例执行的顺序(正常执行顺序是按asc码云运行的,)

    pip install pytest-ordering
    加装饰器 @pytest.mark.run(order=x)
    order值全正或全负时,值越小,优先级越高
    正负数都有时,正数优先级高

    参考连接:
    https://www.cnblogs.com/liuchunxiao83/p/14675063.html

    相关文章

      网友评论

          本文标题:pytest常用配置文件之pytest.ini

          本文链接:https://www.haomeiwen.com/subject/krpasrtx.html