跳转到主要内容

一个flake8扩展,用于检查测试额外样式

项目描述

flake8-fine-pytest

Build Status Maintainability Test Coverage PyPI version PyPI - Python Version

flake8的扩展,用于验证测试结构、额外样式和可读性。

目前我们的检查器

  1. 验证了原因的存在
@pytest.mark.xfail(reason='Super annoying test, fix it later')

这有助于每个人轻松地了解最初的问题是什么,并减少在修复xfailed测试上浪费的时间。

  1. 验证测试模块是否位于描述的目录中。可以在setup.cfg文件中配置
allowed_test_directories = test_unit,test_integration,test_api

如果以test_为前缀的文件不在允许的目录列表中,将引发错误

tests/test_models.py:0:1: FP003 File tests/test_models.py is in the wrong directory.
Allowed directories: test_unit,test_integration,test_api,test_migration
  1. 验证测试函数有一个不太复杂的签名。测试可以配置的参数数量可以在setup.cfg文件中配置
allowed_test_arguments_count = 6

如果测试函数的签名太复杂,将引发错误

tests/test_integration/test_models.py:64:1: FP004 test_save_method has too complex
signature. Allowed count of arguments is 6
  1. 验证测试函数有一个不太复杂的断言块。测试可以配置的断言数量可以在setup.cfg文件中配置
allowed_assert_count = 6

如果测试函数的断言块太复杂,将引发错误

tests/test_integration/test_models.py:64:1: FP005 test_save_method has
too many assert statements. Allowed count of asserts is 6
  1. 验证xfail装饰器有直到参数。直到参数必须指定为有效的datetime.date值,并且不早于当前日期。例如

@pytest.mark.xfail(reason='Test', until=date(2020, 9, 7))

如果xfail没有这样的标记,flake8将引发错误

tests/test_unit/test_utils.py:128:1: FP006 xfail mark has wrong format.
It should has `until` argument

在你忘记指定until参数的情况下

tests/test_unit/test_utils.py:128:1: FP007 xfail mark has wrong format.
It should has `until` argument with datetime.date type

在你指定了错误格式的情况下

tests/test_unit/test_utils.py:128:1: FP008 stale xfail mark

在你有太旧的xfail标记的情况下

  1. 验证测试函数使用唯一名称

  2. 验证测试函数对那些未直接在测试体中引用的固定项使用pytest.mark.usefixtures

例如,检查此函数

# file: test_something.py
def test_something(fixture_one, fixture_two):
    assert fixture_two.some_attribute is not None

将引发

tests/test_unit/test_something.py:2:0: FP010 test_something should use fixtures
as follows: @pytest.mark.usefixtures('fixture_one')

安装

pip install flake8-fine-pytest

示例

示例文件

# test.py

@pytest.mark.xfail(reason='')
def test_xfail() -> None:
    pass

@pytest.mark.xfail
def test_xfail() -> None:
    pass

用法

$ flake8 test.py
test.py:1:1: FP001 xfailed test with empty reason
test.py:5:1: FP002 xfailed test without reason

贡献

我们非常希望您为我们的项目做出贡献。这很简单

  1. 创建一个带有您发现的错误或建议的问题。等待维护者的批准。
  2. 创建一个拉取请求。确保所有检查都是绿色的。
  3. 如果有任何审查评论,请修复。
  4. 做得好。

以下是一些有用的提示

项目详情


下载文件

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

源分发

flake8_fine_pytest-1.0.3.tar.gz (11.1 kB 查看哈希值)

上传时间

构建分发

flake8_fine_pytest-1.0.3-py3-none-any.whl (13.5 kB 查看哈希值)

上传时间 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面