zoukankan      html  css  js  c++  java
  • Pytest权威教程-更改标准(Python)测试发现

    更改标准(Python)测试发现

    在测试收集过程中忽略路径

    通过--ignore=path在cli上传递选项,可以轻松地在收集过程中忽略某些测试目录和模块。pytest允许多个 --ignore选项。例:

    tests/
    |-- example
    |   |-- test_example_01.py
    |   |-- test_example_02.py
    |   '-- test_example_03.py
    |-- foobar
    |   |-- test_foobar_01.py
    |   |-- test_foobar_02.py
    |   '-- test_foobar_03.py
    '-- hello
        '-- world
            |-- test_world_01.py
            |-- test_world_02.py
            '-- test_world_03.py
    

    现在,如果你调用pytest使用,你会发现只收集测试模块,这不符合指定的模式:--ignore=tests/foobar/test_foobar_03.py --ignore=tests/hello/pytest

    =========================== test session starts ============================
    platform linux -- Python 3.x.y, pytest-5.x.y, py-1.x.y, pluggy-0.x.y
    rootdir: $REGENDOC_TMPDIR, inifile:
    collected 5 items
    
    tests/example/test_example_01.py .                                   [ 20%]
    tests/example/test_example_02.py .                                   [ 40%]
    tests/example/test_example_03.py .                                   [ 60%]
    tests/foobar/test_foobar_01.py .                                     [ 80%]
    tests/foobar/test_foobar_02.py .                                     [100%]
    
    ========================= 5 passed in 0.02 seconds =========================
    

    该--ignore-glob选项允许忽略基于Unix Shell样式通配符的测试文件路径。如果要排除测试模块与终端_01.py,执行pytest与--ignore-glob='*_01.py'。

    测试期间收集的测试取消

    通过传递--deselect=item选项,可以在收集过程中分别取消选择测试。例如,说tests/foobar/test_foobar_01.pycontains test_a和test_b。您可以运行全部在测试tests/ 除了对tests/foobar/test_foobar_01.py::test_a 通过调用pytest带。 允许多个选项。--deselect tests/foobar/test_foobar_01.py::test_apytest--deselect

    保留从命令行指定的重复路径

    的默认行为pytest是忽略从命令行指定的重复路径。例:

    pytest path_a path_a
    
    ...
    collected 1 item
    ...
    

    只需收集一次测试。

    要收集重复的测试,请使用--keep-duplicatescli上的选项。例:

    pytest --keep-duplicates path_a path_a
    
    ...
    collected 2 items
    ...
    

    由于收集器仅适用于目录,因此,如果您为单个测试文件指定两次,则无论是否指定,pytest仍将收集两次--keep-duplicates。例:

    pytest test_a.py test_a.py

    ...
    collected 2 items
    ...

    更改目录递归

    你可以norecursedirs在ini文件中设置该选项,例如pytest.ini在项目根目录中:

    # content of pytest.ini
    [pytest]
    norecursedirs = .svn _build tmp*
    

    这将告诉pytest您不要递归到典型的subversion或sphinx-build目录或任何带tmp前缀的目录中。

    更改命名约定

    您可以通过设置配置不同的命名约定python_files,python_classes和 python_functions配置选项。这是一个例子:

    # content of pytest.ini
    # Example 1: have pytest look for "check" instead of "test"
    # can also be defined in tox.ini or setup.cfg file, although the section
    # name in setup.cfg files should be "tool:pytest"
    [pytest]
    python_files = check_*.py
    python_classes = Check
    python_functions = *_check
    

    这将pytest在与glob-pattern 匹配的文件,类中的前缀以及match的函数和方法中寻找测试。例如,如果我们有:check_* .pyCheck*_check

    # content of check_myapp.py
    class CheckMyApp:
        def simple_check(self):
            pass
    
        def complex_check(self):
            pass
    

    测试集合如下所示:

    $ pytest --collect-only
    =========================== test session starts ============================
    platform linux -- Python 3.x.y, pytest-5.x.y, py-1.x.y, pluggy-0.x.y
    cachedir: $PYTHON_PREFIX/.pytest_cache
    rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini
    collected 2 items
    <Module check_myapp.py>
      <Class CheckMyApp>
          <Function simple_check>
          <Function complex_check>
    
    ========================== no tests ran in 0.12s ===========================
    

    你可以通过在模式之间添加空格来检查多个glob模式:

    # Example 2: have pytest look for files with "test" and "example"
    # content of pytest.ini, tox.ini, or setup.cfg file (replace "pytest"
    # with "tool:pytest" for setup.cfg)
    [pytest]
    python_files = test_*.py example_*.py
    

    注意:在python_functions和python_classes选项有没有效果unittest.TestCase测试发现,因为测试用例方法pytest代表发现到单元测试代码。

    将cmdline参数解释为Python包

    你可以使用该--pyargs选项pytest尝试将参数解释为python包名称,派生其文件系统路径,然后运行测试。例如,如果您安装了unittest2,则可以键入:

    pytest --pyargs unittest2.test.test_skipping -q
    

    它将运行相应的测试模块。与其他选项一样,通过ini文件和addopts选项,您可以更永久地进行此更改:

    # content of pytest.ini
    [pytest]
    addopts = --pyargs
    

    现在,一个简单的调用将检查NAME是否作为可导入的程序包/模块存在,否则将其视为文件系统路径。pytest NAME

    找出收集的东西

    你始终可以查看集合树,而无需运行如下测试:

    . $ pytest --collect-only pythoncollection.py
    =========================== test session starts ============================
    platform linux -- Python 3.x.y, pytest-5.x.y, py-1.x.y, pluggy-0.x.y
    cachedir: $PYTHON_PREFIX/.pytest_cache
    rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini
    collected 3 items
    <Module CWD/pythoncollection.py>
      <Function test_function>
      <Class TestClass>
          <Function test_method>
          <Function test_anothermethod>
    
    ========================== no tests ran in 0.12s ===========================
    

    自定义测试集

    你可以轻松地指示pytest从每个Python文件中发现测试:

    # content of pytest.ini
    [pytest]
    python_files = *.py
    

    但是,许多项目将具有setup.py不希望导入的项目。此外,可能只有特定的python版本可以导入文件。在这种情况下,您可以通过在conftest.py文件中列出来动态定义要忽略的文件:

    # content of conftest.py
    import sys
    
    collect_ignore = ["setup.py"]
    if sys.version_info[0] > 2:
        collect_ignore.append("pkg/module_py2.py")
    

    然后,如果您有这样的模块文件:

    # content of pkg/module_py2.py
    def test_only_on_python2():
        try:
            assert 0
        except Exception, e:
            pass
    

    和一个setup.py虚拟文件,像这样:

    # content of setup.py
    0 / 0  # will raise exception if imported
    

    如果您使用Python 2解释器运行,则将找到一个测试并忽略setup.py文件:

    #$ pytest --collect-only
    ====== test session starts ======
    platform linux2 -- Python 2.7.10, pytest-2.9.1, py-1.4.31, pluggy-0.3.1
    rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini
    collected 1 items
    <Module 'pkg/module_py2.py'>
      <Function 'test_only_on_python2'>
    
    ====== no tests ran in 0.04 seconds ======
    

    如果您使用Python 3解释器运行,则一次测试和setup.py 文件都将被忽略:

    $ pytest --collect-only
    =========================== test session starts ============================
    platform linux -- Python 3.x.y, pytest-5.x.y, py-1.x.y, pluggy-0.x.y
    cachedir: $PYTHON_PREFIX/.pytest_cache
    rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini
    collected 0 items
    
    ========================== no tests ran in 0.12s ===========================
    

    通过向中添加模式,也可以忽略基于Unix Shell样式通配符的文件collect_ignore_glob。

    以下示例将conftest.py忽略该文件,setup.py并忽略*_py2.py使用Python 3解释器执行时以结尾的所有文件:

    # content of conftest.py
    import sys
    
    collect_ignore = ["setup.py"]
    if sys.version_info[0] > 2:
        collect_ignore_glob = ["*_py2.py"]
    
  • 相关阅读:
    Java代理(jdk静态代理、动态代理和cglib动态代理)
    Hive安装
    Spark 集群安装
    Flume 远程写HDFS
    Spark Idea Maven 开发环境搭建
    oracle 通不过网络的原因
    oracle一些基本问题
    linux-redhat配置yum源
    liunx虚拟机网络连接
    redhat安装jdk、tomcat、mysql
  • 原文地址:https://www.cnblogs.com/superhin/p/11744053.html
Copyright © 2011-2022 走看看