跳转到主要内容

pytest插件,用于向测试输出头部添加诊断信息

项目描述

10.5281/zenodo.5806492 PyPI CI Status

此插件包提供了一种方法,在运行pytest时将有关系统、Python安装和选择依赖项的信息包含在输出头部。它可以与与Astropy项目无关的包一起使用,但针对与Astropy相关的项目进行了优化。

安装

可以使用pip安装pytest-astropy-header插件

$ pip install pytest-astropy-header

也可以从源代码仓库安装最新开发版本

$ git clone https://github.com/astropy/pytest-astropy-header
$ cd pytest-astropy-header
$ pip install .

在任何情况下,插件都将自动注册以与 pytest 一起使用。

用户指南

本包提供的插件使得在运行测试之前包含诊断信息的大标题变得简单,例如:

Running tests in astropy.

Date: 2019-09-02T23:33:43

Platform: Darwin-18.7.0-x86_64-i386-64bit

Executable: /Users/tom/python/dev/bin/python3.7

Full Python Version:
3.7.4 (v3.7.4:e09359112e, Jul  8 2019, 14:54:52)
[Clang 6.0 (clang-600.0.57)]

encodings: sys: utf-8, locale: UTF-8, filesystem: utf-8
byteorder: little
float info: dig: 15, mant_dig: 15

Package versions:
numpy: 1.16.4
scipy: 1.3.0
matplotlib: 3.1.1
h5py: 2.9.0
pandas: 0.24.2
astropy: 4.0.dev25634

Using Astropy options: remote_data: none.

要使插件以任何测试运行方式(例如通过 pytestpackage.test())都能正常工作,最可靠的方法是在您的包内部的一个 conftest.py 文件中添加以下内容

def pytest_configure(config):
    config.option.astropy_header = True

或者 在您的 setup.cfg 中添加以下内容

[tool:pytest]
astropy_header = true

默认情况下,会显示一些包,但您可能想自定义包的显示方式。至于启用插件,要使它与不同的 astropy 版本兼容,最可靠的方法是通过 conftest.py 文件

try:
    from pytest_astropy_header.display import PYTEST_HEADER_MODULES, TESTED_VERSIONS
except ImportError:  # In case this plugin is not installed
    PYTEST_HEADER_MODULES = {}
    TESTED_VERSIONS = {}

# This really depends on how you set up your package version,
# modify as needed.
from mypackage import __version__ as version

def pytest_configure(config):
    config.option.astropy_header = True  # If you do not have it in setup.cfg
    PYTEST_HEADER_MODULES.pop('Pandas')
    PYTEST_HEADER_MODULES['scikit-image'] = 'skimage'
    TESTED_VERSIONS['mypackage'] = version

PYTEST_HEADER_MODULES 的键应该是大标题中显示的名称,值应该是 Python 模块的名称。

如果您想在标题末尾追加其他文本,可以在您的包中的 conftest.py 文件中实现自己的 pytest_report_header() 函数。例如,要在 Astropy 标题末尾添加自定义页脚,您可以定义

def pytest_report_header(config):
    footer = ("This is some custom text that will appear after the "
              "Astropy pytest header!")
    return footer + "\n"

从 astropy 标题插件迁移到 pytest-astropy-header

注意:pytest-astropy-header 不再支持 astropy<4。本节仅保留为历史原因。

在 astropy 核心包的 v4.0 版本发布之前,上述测试输出大标题处理的插件位于 astropy.tests.plugins.display 中。现在需要几个步骤来更新包以确保只使用 pytest-astropy-header 版本。除了前一部分中提到的配置之外,还应执行以下操作。

首先,您应该可以通过例如替换以下内容来显著简化 conftest.py 文件:

from astropy.version import version as astropy_version
if astropy_version < '3.0':
    # With older versions of Astropy, we actually need to import the pytest
    # plugins themselves in order to make them discoverable by pytest.
    from astropy.tests.pytest_plugins import *
else:
    # As of Astropy 3.0, the pytest plugins provided by Astropy are
    # automatically made available when Astropy is installed. This means it's
    # not necessary to import them here, but we still need to import global
    # variables that are used for configuration.
    from astropy.tests.plugins.display import (pytest_report_header,
                                               PYTEST_HEADER_MODULES,
                                               TESTED_VERSIONS)

# Customize the following lines to add/remove entries from
# the list of packages for which version numbers are displayed when running
# the tests. Making it pass for KeyError is essential in some cases when
# the package uses other astropy affiliated packages.
try:
    PYTEST_HEADER_MODULES['Astropy'] = 'astropy'
    del PYTEST_HEADER_MODULES['h5py']
except KeyError:
    pass

# This is to figure out the package version, rather than
# using Astropy's
from .version import version, astropy_helpers_version

packagename = os.path.basename(os.path.dirname(__file__))
TESTED_VERSIONS[packagename] = version
TESTED_VERSIONS['astropy_helpers'] = astropy_helpers_version

例如替换为:

import os

from astropy.version import version as astropy_version
if astropy_version < '3.0':
    from astropy.tests.pytest_plugins import *
    del pytest_report_header
else:
    from pytest_astropy_header.display import PYTEST_HEADER_MODULES, TESTED_VERSIONS


def pytest_configure(config):

    config.option.astropy_header = True

    PYTEST_HEADER_MODULES.pop('Pandas', None)
    PYTEST_HEADER_MODULES['scikit-image'] = 'skimage'

    from .version import version, astropy_helpers_version
    packagename = os.path.basename(os.path.dirname(__file__))
    TESTED_VERSIONS[packagename] = version
    TESTED_VERSIONS['astropy_helpers'] = astropy_helpers_version

请注意,虽然您需要使用最新的 pytest-astropy 版本来实现这一点,但它应与从 Astropy 2.0 开始的版本一起工作,而无需所有 try...except 导入。

接下来检查所有的 conftest.py 文件,并确保从如下列表中删除旧插件:

pytest_plugins = [
  'astropy.tests.plugins.display',
]

开发状态

有关问题、错误报告和功能请求,可以在 github 上提交。

许可证

本包采用 3 条款 BSD 风格许可 - 请参阅 LICENSE.rst 文件。

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分布

pytest-astropy-header-0.2.2.tar.gz (9.9 kB 查看哈希值)

上传时间

构建分布

pytest_astropy_header-0.2.2-py3-none-any.whl (7.8 kB 查看哈希值)

上传时间 Python 3