python-3.x - 从 tox 运行时找不到夹具“mocker”(pytest-mock)
问题描述
我一直在使用pytest-mock库来模拟 pytest。当我尝试使用tox
命令运行测试时,出现以下错误:
...
tests/test_cli.py ....EEEE
...
file /path/to/test_cli.py, line 63
def test_cli_with_init_cmd_fails_with_db_error(runner, mocker, context):
E fixture 'mocker' not found
> available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, context, cov, doctest_namespace, fs, monkeypatch, no_cover, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, requests_mock, runner, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
> use 'pytest --fixtures [testpath]' for help on them.
但是,当我尝试从我的 venv 中直接使用 pytest 运行测试时,一切都按预期工作。
$ py.test --cov esmigrate --cov-report term-missing
...
platform linux -- Python 3.8.5, pytest-6.1.1, py-1.9.0, pluggy-0.13.1
rootdir: /path/to/project/root, configfile: tox.ini
plugins: cov-2.10.1, pyfakefs-4.0.2, mock-3.3.1, requests-mock-1.8.0
collected 50 items
tests/test_cli.py ........ [ 16%]
tests/test_contexts/test_context_config.py ... [ 22%]
tests/test_internals/test_db_manager.py .......... [ 42%]
tests/test_internals/test_glob_loader.py ..... [ 52%]
tests/test_internals/test_http_handler.py ....... [ 66%]
tests/test_internals/test_script_parser.py ................. [100%]
...
这很奇怪,因为我已经pytest-mock
在我的文件中添加了该requirements.txt
文件,该文件用于在 venv 中安装依赖项,并且我也将此文件添加为 tox testenv 的依赖项。这是我tox.ini
文件的内容。
[tox]
envlist=py36, py37, py38, flake8
[pytest]
filterwarnings =
error::DeprecationWarning
error::PendingDeprecationWarning
[flake8]
max-line-length = 120
select = B,C,E,F,W,T4,B9,B950
ignore = E203,E266,E501,W503,D1
[testenv]
passenv=USERNAME
commands=py.test --cov esmigrate {posargs} --cov-report term-missing
deps= -rrequirements.txt
[testenv:flake8]
basepython = python3.8
deps =
flake8
commands =
flake8 esmigrate tests
requirements.txt
文件快照
...
pyfakefs==4.0.2
pyparsing==2.4.7
pyrsistent==0.17.3
pytest==6.1.1
pytest-cov==2.10.1
pytest-mock==3.3.1
PyYAML==5.3.1
...
从任何一个运行时都不会引起任何问题travis-ci
,但我想知道这里有什么问题以及我做错了什么。是 tox-env 无法安装 pytest-mock,还是“mocker”夹具被其他东西遮蔽了?
解决方案
tox 当前(尽管计划在(撰写本文时)当前重写中进行改进)如果文件不管理更改(例如 requirements.txt / setup.py),则不会重新创建环境
对于相关问题,您可以查看我的问题和解决方法
这里的核心问题是,如果您没有直接内联管理 tox 环境依赖项,tox.ini
它将不会注意到更改(例如添加/删除依赖requirements.txt
项),因此您需要运行带有--recreate
标志的 tox 以反映这些更改
免责声明:我是当前的 tox 维护者之一
推荐阅读
- reactjs - MUI 图标:找不到模块:无法解析“.../@mui/styled-engine”中的“@emotion/react”
- nginx - 在 NGINX 中启用 wss 连接
- android - 带有组的插件的 Gradle 版本目录
- oracle - 如何在 oracle 中通过 dblink 将数组参数发送到远程
- java - Firestore:如何使字段免于通过 Java 代码编制索引
- android - BLE 电池电量服务不读取手机电池电量
- vue.js - Vuetify - 使用范围 CSS 覆盖模板插槽
- react-native - React Native - 本地存储(sqlite)与实时服务器同步
- javascript - 编写一个函数,该函数通过调用另一个函数来返回一个值并将其存储在一个新数组中,从而获取一个数组
- freeze - 在 android 中打开最大化或通知抽屉时,Ionic 5 应用程序无响应或冻结