更改标准(Python)测试发现

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

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

  1. tests/
  2. |-- example
  3. | |-- test_example_01.py
  4. | |-- test_example_02.py
  5. | '-- test_example_03.py
  6. |-- foobar
  7. | |-- test_foobar_01.py
  8. | |-- test_foobar_02.py
  9. | '-- test_foobar_03.py
  10. '-- hello
  11. '-- world
  12. |-- test_world_01.py
  13. |-- test_world_02.py
  14. '-- test_world_03.py

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

  1. =========================== test session starts ============================
  2. platform linux -- Python 3.x.y, pytest-5.x.y, py-1.x.y, pluggy-0.x.y
  3. rootdir: $REGENDOC_TMPDIR, inifile:
  4. collected 5 items
  5. tests/example/test_example_01.py . [ 20%]
  6. tests/example/test_example_02.py . [ 40%]
  7. tests/example/test_example_03.py . [ 60%]
  8. tests/foobar/test_foobar_01.py . [ 80%]
  9. tests/foobar/test_foobar_02.py . [100%]
  10. ========================= 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是忽略从命令行指定的重复路径。例:

  1. pytest path_a path_a
  2. ...
  3. collected 1 item
  4. ...

只需收集一次测试。

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

  1. pytest --keep-duplicates path_a path_a
  2. ...
  3. collected 2 items
  4. ...

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

pytest test_a.py test_a.py

...

collected 2 items

...

更改目录递归

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

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

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

更改命名约定

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

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

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

  1. # content of check_myapp.py
  2. class CheckMyApp:
  3. def simple_check(self):
  4. pass
  5. def complex_check(self):
  6. pass

测试集合如下所示:

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

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

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

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

将cmdline参数解释为Python包

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

  1. pytest --pyargs unittest2.test.test_skipping -q

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

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

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

找出收集的东西

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

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

自定义测试集

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

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

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

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

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

  1. # content of pkg/module_py2.py
  2. def test_only_on_python2():
  3. try:
  4. assert 0
  5. except Exception, e:
  6. pass

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

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

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

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

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

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

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

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

  1. # content of conftest.py
  2. import sys
  3. collect_ignore = ["setup.py"]
  4. if sys.version_info[0] > 2:
  5. collect_ignore_glob = ["*_py2.py"]

Pytest权威教程-更改标准(Python)测试发现的更多相关文章

  1. Pytest权威教程11-模块及测试文件中集成doctest测试

    目录 模块及测试文件中集成doctest测试 编码 使用doctest选项 输出格式 pytest-specific 特性 返回: Pytest权威教程 模块及测试文件中集成doctest测试 编码 ...

  2. Pytest权威教程22-优质集成实践

    目录 优质集成实践 使用pip安装包 Python测试发现的约定 选择测试布局结构/导入规则 在应用程序代码外测试 测试作为应用程序代码的一部分 tox 返回: Pytest权威教程 优质集成实践 使 ...

  3. Pytest权威教程26-示例和自定义技巧

    目录 示例和自定义技巧 返回: Pytest权威教程 示例和自定义技巧 这是一个(不断增长的)示例列表.如果你需要更多示例或有疑问,请联系我们.另请参阅包含许多示例代码段的 综合文档.此外,stack ...

  4. Pytest权威教程(官方教程翻译)

    Pytest权威教程01-安装及入门 Pytest权威教程02-Pytest 使用及调用方法 Pytest权威教程03-原有TestSuite的执行方法 Pytest权威教程04-断言的编写和报告 P ...

  5. Pytest权威教程09-捕获标准输出及标准错误输出

    目录 捕获标准输出及标准错误输出 默认 stdout/stderr/stdin 捕获行为 设置捕获方法或禁用捕获 调试中使用print语句 在测试用例中使用的捕获的输出 返回: Pytest权威教程 ...

  6. Pytest权威教程23-不稳定测试

    目录 不稳定测试用例处理 为什么不稳定测试是个问题 潜在的根本原因 Pytest特性 其他一般策略 相关研究 相关资源 返回: Pytest权威教程 不稳定测试用例处理 "不稳定" ...

  7. Pytest权威教程21-API参考-07-配置选项(Configuration Options)

    目录 配置选项(Configuration Options) addopts cache_dir confcutdir console_output_style doctest_encoding do ...

  8. Pytest权威教程21-API参考-05-对象(Objects)

    目录 对象(Objects) CallInfo Class Collector Config ExceptionInfo FixtureDef FSCollector Function Item Ma ...

  9. Pytest权威教程21-API参考-04-钩子(Hooks)

    目录 钩子(Hooks) 引导时的Hook方法 初始化时的Hook方法 测试运行时的Hook方法 收集用例时的Hook方法 生成测试结果时的Hook方法 调试/交互Hook方法 返回: Pytest权 ...

随机推荐

  1. NEST 多IndexType与分页

    /// <summary> /// POST /_all/employee/_search?typed_keys=true /// </summary> public void ...

  2. NEST 根据id查询

    想要在NEST里根据id查询 GET /employee/employee/1 可使用Get方法 public IGetResponse<employee> GetDoc() { var ...

  3. Libev库学习

    Libev库学习 https://www.cnblogs.com/wunaozai/p/3950249.html Libev库学习(1)https://www.cnblogs.com/wunaozai ...

  4. MySql注释的写法

    每一种语言都有它的注释方式,代码量少的时候还可以,随着代码量越来越多,代码注释的重要性也越发凸显. 在mysql中主要有三种方式: 1.常用的方式,跟在css中那些注释一样 :/* 内容 */ /* ...

  5. python day 12: 选课系统

    目录 python day 12 1. 通过类来创建选课系统 1.1 类库models.py 2. 配置文件setting.py 3. administrator.py 4. student.py p ...

  6. OS UIButton之UIButtonType详解-转

    我做了一个关于UIButtonType的Demo,效果如下图: UIButtonType各个类型的解释: typedef NS_ENUM(NSInteger, UIButtonType) { UIBu ...

  7. Map转url ,url转Map工具类

    /** * 将url参数转换成map * @param param aa=11&bb=22&cc=33 * @return */ public static Map<String ...

  8. Kubernetes- Dashboard 部署

    获取dashboard 的yaml文件 wget wget https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/dep ...

  9. 专心学LINUX:CentOS关闭屏幕自动锁定和睡眠

    在VMware中学习CentOS总免不了一直测试.调试,加上看书.刨坛,再转回到CentOS界面时已经被锁定了.看看怎么将这定时锁定取消以免麻烦.虽然可以使用字符终端,但字符终端不便于翻看前面已经发出 ...

  10. centos7单用户模式

    1 - 在启动grub菜单,选择编辑选项启动 2 - 按键盘e键,来进入编辑界面 3 - 找到Linux 16的那一行,将ro改为rw init=/sysroot/bin/sh 如果用的是kvm做了c ...