• python-pytest学习(十四)配置文件pytest.ini


    一、前言

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

    二、ini配置文件

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

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

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

      (3)init.py识别该文件夹为python的package包

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

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

    ini文件基本格式

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

    使用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

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

    三、mark标记

    如下案例,使用了2个标签:webtest和hello,使用mark标记功能对于以后分类测试非常有用处

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

    运行结果:

    ============================= test session starts =============================
    platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0 -- D:softpython3.6python.exe
    cachedir: .pytest_cache
    metadata: {'Python': '3.6.0', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '3.6.3', 'py': '1.5.4', 'pluggy': '0.6.0'}, 'Plugins': {'metadata': '1.7.0', 'html': '1.19.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'D:\soft\jdk18\jdk18v'}
    rootdir: D:YOYO, inifile:
    plugins: metadata-1.7.0, html-1.19.0, allure-adaptor-1.7.10
    collecting ... collected 5 items / 3 deselected
    
    test_mark.py::TestClass::test_01 PASSED                                  [ 50%]
    test_mark.py::TestClass::test_02 PASSED                                  [100%]
    
    =================== 2 passed, 3 deselected in 0.11 seconds ====================

      有时候标签多了,不容易记住,为了方便后续执行指令的时候能准确使用mark的标签,可以写入到pytest.ini文件

    # pytest.ini
    [pytest]
    
    markers = 
        webtest: Run the webtest case
        hello: Run the hello case

      标记好之后,可以使用pytest --markers查看到

    pytest --markers

    四、禁用xpass

      设置xfail_strict = true可以让那些标记为@pytest.mark.xfail但实际通过的测试用例被报告为失败,什么叫标记为@pytest.mark.xfail但实际通过,这个比较绕脑,看以下案例

    import pytest
    
    def test_hello():
        print("hello world!")
        assert 1
    
    @pytest.mark.xfail()
    def test_01():
        a = "hello"
        b = "hello world"
        assert a == b
    
    @pytest.mark.xfail()
    def test_02():
        a = "hello"
        b = "hello world"
        assert a != b
    
    if __name__=="__main__":
        pytest.main(["-v","test_xpass.py"])

      运行结果:

    collecting ... collected 3 items
    
    test_xpass.py::test_hello PASSED    [ 33%]
    test_xpass.py::test_01 xfail     [ 66%]
    test_xpass.py::test_02 XPASS     [100%]
    
    =============== 1 passed, 1 xfailed, 1 xpassed in 0.27 seconds ================

      test_01和test_02这2个用例一个是a == b一个是a != b,两个都标记失败了,我们希望两个用例不用执行全部显示xfail。实际上最后一个却显示xpass。为了让两个都显示xfail,那就加个配置xfail_strict = true

    # 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_01 xfail         [ 66%]
    test_xpass.py::test_02 FAILED        [100%]
    
    ================================== FAILURES ===================================
    _________________________________ test_yoyo2 __________________________________
    [XPASS(strict)] 
    ================ 1 failed, 1 passed, 1 xfailed in 0.05 seconds ================

    参考文章:https://www.jianshu.com/p/a613a3a4d030

  • 相关阅读:
    错误及异常处理-[PathTooLongException]指定的路径或文件名太长
    在线颜色转换器
    文件被锁定的原因
    EF6官方文档
    我关注的
    "此实现不是 Windows 平台 FIPS 验证的加密算法的一部分"解决办法
    Rsync Error set gid failed rsync error
    转载——EntiyFrameWork教程
    Word 出现“因为没有打开的文档,所以这一命令无效”的错误
    Winsock网络编程笔记:基于UDP的Client
  • 原文地址:https://www.cnblogs.com/zhaocbbb/p/12852476.html
Copyright © 2020-2023  润新知