pytest插件,用于编写mypy插件的测试
项目描述
pytest插件,用于测试mypy类型、存根和插件
安装
此软件包可在PyPI
pip install pytest-mypy-plugins
conda install -c conda-forge pytest-mypy-plugins
用法
运行
插件安装后,将自动由pytest
拾取,因此只需执行即可
pytest
路径
如果设置了PYTHONPATH
和MYPYPATH
环境变量,它们将在调用时传递给mypy
。如果您正在测试本地插件并需要提供导入路径,这可能很有用。
请注意,当在子进程(默认)中运行mypy
时,测试用例将在临时工作目录中运行,其中相对路径(例如PYTHONPATH=./my_plugin
)不引用您运行pytest
的目录。如果遇到这种情况,请考虑使用带有--mypy-same-process
的pytest
或使路径绝对,例如PYTHONPATH=$(pwd)/my_plugin pytest
。
您还可以在yml
规范的env:
部分中指定PYTHONPATH
、MYPYPATH
或任何其他环境变量
- case: mypy_path_from_env
main: |
from pair import Pair
instance: Pair
reveal_type(instance) # N: Revealed type is 'pair.Pair'
env:
- MYPYPATH=../fixtures
什么是测试用例?
通常,每个测试用例都是一个以正确格式编写的 YAML
文件中的数组元素。在此基础上,每个用例必须符合以下类型
属性 | 类型 | 描述 |
---|---|---|
case |
str |
测试用例的名称,符合 [a-zA-Z0-9] 模式 |
main |
str |
类似于写入 .py 文件的代码段 |
files |
Optional[List[File]]=[] * |
可选:需要时模拟导入的额外文件列表 |
disable_cache |
Optional[bool]=False |
设置为 true 将禁用 mypy 缓存 |
mypy_config |
Optional[str] |
内联 mypy 配置,直接作为 --config-file 选项传递给 mypy ,可能还会与 --mypy-pyproject-toml-file 或 --mypy-ini-file 的内容一起传递。默认情况下视为 ini ,只有当传递了 --mypy-pyproject-toml-file 时才视为 toml |
env |
Optional[Dict[str, str]]={} |
测试运行过程中提供的环境变量 |
parametrized |
Optional[List[Parameter]]=[] * |
参数列表,类似于 @pytest.mark.parametrize |
skip |
str |
使用以下全局变量评估的表达式: sys 、os 、pytest 和 platform |
expect_fail |
bool |
将测试用例标记为预期失败,类似于 @pytest.mark.xfail |
regex |
str |
允许在注释中使用正则表达式与实际输出进行匹配。默认为 "no",即匹配全文。 |
(*) 上文使用的伪类型的附录
class File:
path: str
content: Optional[str] = None
Parameter = Mapping[str, Any]
实现说明
main
必须是非空字符串,能够评估为有效的 Python 代码,- 每个额外文件的
content
必须能够评估为有效的 Python 代码, parametrized
条目必须都是相同 类型 的对象。这意味着每个条目都必须有 完全相同 的键集,skip
- 在skip
中设置的表达式直接传递给eval
。建议了解一下eval
的工作原理。
仓库还提供了一个 JSONSchema,用于验证输入。它还可以为您的编辑器提供自动完成、描述和验证。
您只需在 YAML 文件的顶部添加以下行
# yaml-language-server: $schema=https://raw.githubusercontent.com/typeddjango/pytest-mypy-plugins/master/pytest_mypy_plugins/schema.json
示例
1. 内联类型期望
# typesafety/test_request.yml
- case: request_object_has_user_of_type_auth_user_model
main: |
from django.http.request import HttpRequest
reveal_type(HttpRequest().user) # N: Revealed type is 'myapp.models.MyUser'
# check that other fields work ok
reveal_type(HttpRequest().method) # N: Revealed type is 'Union[builtins.str, None]'
files:
- path: myapp/__init__.py
- path: myapp/models.py
content: |
from django.db import models
class MyUser(models.Model):
pass
2. @parametrized
- case: with_params
parametrized:
- val: 1
rt: builtins.int
- val: 1.0
rt: builtins.float
main: |
reveal_type({{ val }}) # N: Revealed type is '{{ rt }}'
可以参数化的属性
main
mypy_config
out
3. 更长的类型期望
- case: with_out
main: |
reveal_type('abc')
out: |
main:1: note: Revealed type is 'builtins.str'
4. 期望中的正则表达式
- case: expected_message_regex_with_out
regex: yes
main: |
a = 'abc'
reveal_type(a)
out: |
main:2: note: .*str.*
5. 输出特定行的正则表达式。
- case: expected_single_message_regex
main: |
a = 'hello'
reveal_type(a) # NR: .*str.*
选项
mypy-tests:
--mypy-testing-base=MYPY_TESTING_BASE
Base directory for tests to use
--mypy-pyproject-toml-file=MYPY_PYPROJECT_TOML_FILE
Which `pyproject.toml` file to use
as a default config for tests.
Incompatible with `--mypy-ini-file`
--mypy-ini-file=MYPY_INI_FILE
Which `.ini` file to use as a default config for tests.
Incompatible with `--mypy-pyproject-toml-file`
--mypy-same-process
Run in the same process. Useful for debugging,
will create problems with import cache
--mypy-extension-hook=MYPY_EXTENSION_HOOK
Fully qualified path to the extension hook function,
in case you need custom yaml keys. Has to be top-level
--mypy-only-local-stub
mypy will ignore errors from site-packages
--mypy-closed-schema
Use closed schema to validate YAML test cases,
which won't allow any extra keys
(does not work well with `--mypy-extension-hook`)
进一步阅读
许可证
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关 安装包 的更多信息。
源码分发
pytest-mypy-plugins-3.1.2.tar.gz (26.2 kB 查看哈希值)
构建分发
关闭
pytest-mypy-plugins-3.1.2.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 14fa9b00e59713a6fdb88fcf04e8139b9467e117b98d61fc12038c60afb7febe |
|
MD5 | c4f435169e3d4f7589dadeecd816d0a7 |
|
BLAKE2b-256 | 95365411148fa0ad3242e38364152016a9d56758c0e0808fcb1909562f85b328 |
关闭
pytest_mypy_plugins-3.1.2-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 3478ccb68e26a159e1287c4614c60f84cd0720e3895f174365c7440498234b9f |
|
MD5 | 7a8a5ab349fe53d6776adeef3445675d |
|
BLAKE2b-256 | 6cc2a75ad669251e71204c4bc06386d1d3cf1ff0593f85138ab4ccf28db792da |