微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从tox运行时找不到夹具“ mocker”pytest-mock

如何解决从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)下无法重新创建环境

有关问题,您可以看到我的question and workarounds

这里的核心问题是,如果您不是直接在tox.ini中内联管理毒物环境依赖关系,它将不会注意到更改(例如从requirements.txt添加/删除依赖关系),因此您需要使用--recreate标志运行tox以反映这些更改


免责声明:我是目前的毒物维护者之一

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。