• Pytest07-配置


    7.配置

    7.1 pytest配置文件概念

        pytest常见的非测试文件,如下所示:

    • 1.pytest.ini:pytest的主配置文件,可以改变pytest的默认行为,其中有很多可配置的选项
    • 2.conftest.py:本地的插件库,其中的hook函数和fixture将作用于该文件所在的目录及其子目录
    • 3.__init__:每个测试子目录可能都包含该文件,代表一个包,常用于解决命名冲突

        pytest.ini示例如下所示:

    [pytest]
    addopts = -rsxX -L --tb=short --strict
    xfail_stric = true
    ...
    

    7.1.1 使用pytest --help查看ini文件选项

    >>> pytest --help
    [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 directories are given in
      usefixtures (args):       list of default fixtures to be used with this project
      python_files (args):      glob-style file patterns for Python test module discovery
      python_classes (args):    prefixes or glob names for Python test class discovery
      python_functions (args):  prefixes or glob names for Python test function and method discovery
    

    7.1.2 插件可以添加ini文件选项

        除前面列出的选项,利用插件和conftest.py文件还可以添加新的选项。新增的选项也可以使用pytest --help查看。

    7.2 更改默认命令行选项

        如果某一些选项经常,又不想每次重复输入,这时则可以修改pytest.ini文件的addopts设置,如下所示:

    [pytest]
    addopts = -rsxX -L --tb=short --strict
    

    7.3 注册标记来防止拼写错误

        自定义标记可以简化测试工作,让我们可以使用指定的标记运行某个子集,但标记很容易拼错。默认情况下,并不会引起程序错误,pytest会以为是创建的另外一个新的标记。为避免这种情况,可以在pytest.ini中注册标记。如下所示:

    [pytest]
    markers =
       smoke: Run the smoke test function
       get:   Run the get test function
    

        标记做好之后,可以通过pytest --markers来查看,如下所示:

    >>> pytest --markers
    
    @pytest.mark.smoke: Run the smoke test function
    @pytest.mark.get:   Run the get test function
    

        没有注册的标记不会出现在--markers列表中。如果使用--strict选项,遇到拼写错误的标记或未标记的标记就会报错。

    7.4 指定pytest的最低版本号

        minversion选项可以指定运行测试用例的pytest的最低版本。示例如下所示:

    [pytest]
    minversion = 5.2
    

    7.5 指定pytest忽略某些目录

        pytest在默认情况,会搜索指定目录及其子目录。如果想跳过某些目录,可以使用norecursedirs选项。

    norecursedirs的默认设置是.* build dist CVS _darcs {arch}和*.egg

        示例如下所示:

    [pytest]
    norecursedirs = .* build dist CVS _darcs {arch} *.egg
    

    7.6 指定测试目录

        norecursedirs指定哪些目录不用访问,而 testpaths 则指定了pytest去哪里搜索运行测试, testpaths是一系列相对于根目录的路径,用于限定测试用例的搜索范围,只有在pytest未指定文件目录参数或测试用例标识符时,该选项才会启用。如下所示:

    [pytest]
    testpaths = test
    

    7.7 更改测试搜索的规则

        pytest根据一定的规则搜索并运行测试,一个标准的测试搜索规则如下所示:

    • 1.从一个或多个目录开始查找,如果未指定,则默认为当前目录
    • 2.在该目录及其子目录中查找测试模块,测试模块是以test_.py或_test.py的文件
    • 3.在测试模块中搜索以test_开头的函数名或Test开头的类,且过滤掉包含__init__()的类

    1.python_classes

        通常,pytest的测试搜索规则是寻找以Test*开头的测试类,而且这个类不能包含__init__()方法。但如果没有按照这个格式对类进行命名(如 < something > Test),该怎么办?针对这种情况,可以使用python_classes来解决这个问题。如下所示:

    [pytest]
    python_classes = *Test Test* *Suite
    

    2.python_files

        与python_classes类似,python_files是更改搜索测试模块的规则,如下所示:

    [pytest]
    python_files = test_* *_test check_*
    

    3.python_functions

        与前面两个类似,python_functions是更改搜索测试函数规则,如下所示:

    [pytest]
    python_functions = test_* *_test check_*
    

    7.8 禁用XPASS

        设置xfail_strict = True 可以将那些被标记@pytest.mark.xfail但实际运行通过的测试用例也被报告为测试失败。设置如下所示:

    [pytest]
    xfail_strict = True
    

    7.9 避免文件冲突

        在讲解Python的模块和包时,为了避免命名冲突,可以使用模块和包。在测试过程中,同样也可以同样的解决思路。

    原文地址:https://www.cnblogs.com/surpassme/p/13262604.html

    本文同步在微信订阅号上发布,如各位小伙伴们喜欢我的文章,也可以关注我的微信订阅号:woaitest,或扫描下面的二维码添加关注:
    MyQRCode.jpg

  • 相关阅读:
    PSNR计算
    Latex中的(左边有大括号的)方程组解决方案汇总
    CS (Compressive sensing, 压缩传感)
    OpenCV 2.3.0 编译错误之 file INSTALL cannot find opencv_ffmpeg230d.dll(无法找到opencv_ffmpeg230d.dll)
    Visual Studio 2008 编译程序时的 mt.exe 返回错误
    fontDialog,colorDialog,saveDialog改变字体大小,颜色,以后保存图片文件实例
    Lambda入门示例实例
    C#接口入门基础知识复习
    C#,visual studio 2010中实现最简单DragDrop实例
    抽象类实现条件参数必须和抽象类中的一样
  • 原文地址:https://www.cnblogs.com/surpassme/p/13262604.html
Copyright © 2020-2023  润新知