Tox "ERROR: file not found: <project_name>"

Tox "ERROR: file not found: <project_name>"

我正在运行创建一个基于 Tox basic example and a walkthrough video entitled Automating Build, Test and Release Workflows with tox 的超级简单 tox.ini 文件。问题是每次我 运行 tox 命令时,我都会收到一堆调用错误和“错误:找不到文件:

tox.ini:

# tox (https://tox.readthedocs.io/) is a tool for running tests
# in multiple virtualenvs. This configuration file will run the
# test suite on all supported python versions. To use it, "pip install tox"
# and then run "tox" from this directory.

[tox]
skipsdist = true
envlist = py36, py37, build
skip_missing_interpreters = true

[testenv]
whitelist_externals = *
passenv = *
deps = -rrequirements-test.txt
commands = {[testenv:unit]commands}

[travis]
python =
    3.6: py36
    3.7: py37, build

[testenv:unit]
deps = {[testenv]deps}
commands = pytest {posargs:--cov={envsitepackagesdir}/project_name {envsitepackagesdir}/project_name src/tests}

[testenv:test]
deps =
    {[testenv]deps}
    {[testenv:build]deps}
commands =
    {[testenv:unit]commands}
    {[testenv:build]commands}

[testenv:build]
skip_install = true
deps =
    wheel
commands =
    rm -rf dist build
    python -W ignore setup.py -q sdist bdist_wheel

;[testenv:release]
;deps =
;    {[testenv:build]deps}
;    twine
;commands =
;    {[testenv:build]commands}
;    twine upload dist/*
;

# From https://pytest-cov.readthedocs.io/en/latest/tox.html
[testenv:clean]
deps = coverage
skip_install = true
commands = coverage erase

[pytest]
addopts =
    -v -s
    --color=yes
    --cov
    --cov-append
    --cov-report=term-missing
    --cov-config=setup.cfg

[coverage:run]
omit =
    */tests/*
    */test_*
    */distutils/*
    */site-packages/*

[coverage:report]
show_missing = True

[flake8]
exclude = .tox

[report]
omit = */usr/*

命令行界面:

$ tox
py36 create: /path/to/my/project/python/project_namepy/.tox/py36
py36 installdeps: -rrequirements-test.txt
py36 installed: atomicwrites==1.3.0,attrs==19.1.0,bleach==3.1.0,certifi==2019.6.16,chardet==3.0.4,colorama==0.4.1,coverage==4.5.4,docutils==0.15.2,Faker==2.0.0,filelock==3.0.12,idna==2.8,importlib-metadata==0.19,more-itertools==7.2.0,packaging==19.1,pkginfo==1.5.0.1,pluggy==0.12.0,py==1.8.0,Pygments==2.4.2,pyparsing==2.4.2,pytest==5.0.1,pytest-cov==2.7.1,python-dateutil==2.8.0,readme-renderer==24.0,requests==2.22.0,requests-toolbelt==0.9.1,six==1.12.0,text-unidecode==1.2,toml==0.10.0,tox==3.13.2,tqdm==4.33.0,twine==1.13.0,urllib3==1.25.3,virtualenv==16.7.2,wcwidth==0.1.7,webencodings==0.5.1,zipp==0.5.2
py36 run-test-pre: PYTHONHASHSEED='2972258678'
py36 run-test: commands[0] | pytest --cov=/path/to/my/project/python/project_namepy/.tox/py36/lib/python3.6/site-packages/project_name /path/to/my/project/python/project_namepy/.tox/py36/lib/python3.6/site-packages/project_name src/tests
==================================================================================== test session starts ====================================================================================
platform darwin -- Python 3.6.5, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 -- /usr/local/Cellar/python/3.6.5/bin/python3.6
cachedir: .tox/py36/.pytest_cache
rootdir: /path/to/my/project/python/project_namepy, inifile: tox.ini
plugins: cov-2.7.1
collected 0 items                                                                                                                                                                           

=============================================================================== no tests ran in 0.02 seconds ================================================================================
ERROR: file not found: /path/to/my/project/python/project_namepy/.tox/py36/lib/python3.6/site-packages/project_name

ERROR: InvocationError for command /path/to/my/project/python/project_namepy/.tox/py36/bin/pytest --cov=/path/to/my/project/python/project_namepy/.tox/py36/lib/python3.6/site-packages/project_name /path/to/my/project/python/project_namepy/.tox/py36/lib/python3.6/site-packages/project_name src/tests (exited with code 4)
py37 create: /path/to/my/project/python/project_namepy/.tox/py37
py37 installdeps: -rrequirements-test.txt
py37 installed: atomicwrites==1.3.0,attrs==19.1.0,bleach==3.1.0,certifi==2019.6.16,chardet==3.0.4,colorama==0.4.1,coverage==4.5.4,docutils==0.15.2,Faker==2.0.0,filelock==3.0.12,idna==2.8,importlib-metadata==0.19,more-itertools==7.2.0,packaging==19.1,pkginfo==1.5.0.1,pluggy==0.12.0,py==1.8.0,Pygments==2.4.2,pyparsing==2.4.2,pytest==5.0.1,pytest-cov==2.7.1,python-dateutil==2.8.0,readme-renderer==24.0,requests==2.22.0,requests-toolbelt==0.9.1,six==1.12.0,text-unidecode==1.2,toml==0.10.0,tox==3.13.2,tqdm==4.33.0,twine==1.13.0,urllib3==1.25.3,virtualenv==16.7.2,wcwidth==0.1.7,webencodings==0.5.1,zipp==0.5.2
py37 run-test-pre: PYTHONHASHSEED='2972258678'
py37 run-test: commands[0] | pytest --cov=/path/to/my/project/python/project_namepy/.tox/py37/lib/python3.7/site-packages/project_name /path/to/my/project/python/project_namepy/.tox/py37/lib/python3.7/site-packages/project_name src/tests
==================================================================================== test session starts ====================================================================================
platform darwin -- Python 3.7.4, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 -- /usr/local/Cellar/python/3.6.5/bin/python3.6
cachedir: .tox/py37/.pytest_cache
rootdir: /path/to/my/project/python/project_namepy, inifile: tox.ini
plugins: cov-2.7.1
collected 0 items                                                                                                                                                                           

=============================================================================== no tests ran in 0.01 seconds ================================================================================
ERROR: file not found: /path/to/my/project/python/project_namepy/.tox/py37/lib/python3.7/site-packages/project_name

ERROR: InvocationError for command /path/to/my/project/python/project_namepy/.tox/py37/bin/pytest --cov=/path/to/my/project/python/project_namepy/.tox/py37/lib/python3.7/site-packages/project_name /path/to/my/project/python/project_namepy/.tox/py37/lib/python3.7/site-packages/project_name src/tests (exited with code 4)
build create: /path/to/my/project/python/project_namepy/.tox/build
build installdeps: wheel
build run-test-pre: PYTHONHASHSEED='2972258678'
build run-test: commands[0] | rm -rf dist build
build run-test: commands[1] | python -W ignore setup.py -q sdist bdist_wheel
__________________________________________________________________________________________ summary __________________________________________________________________________________________
ERROR:   py36: commands failed
ERROR:   py37: commands failed
  build: commands succeeded

aforementioned video 期间,tox.ini 设置中的第一行项目之一是编写与跳过 sdist 的创建有关的行,这被认为是 "expensive":

[tox]
skipsdist = true

事实证明,如果您的测试依赖于导入包,这是非常重要的一步(提示:他们几乎必须这样做。否则,他们在测试什么?)。视频的解说员(也是该项目的主要贡献者)确实提到在 "development" 模式下安装包 (pip install -e .),但忽略了(或者我可能听错了)包实际上需要在某个时候安装。

将此值更改为 skipsdist = false 或简单地删除该行可完全解决问题。