测试因 tox 而失败,因为无法找到被测模块

Tests fail with tox because module under test can't be found

我在毒物方面遇到了一些麻烦。测试失败 运行 因为找不到被测模块。根据我所做的挖掘,我怀疑问题可能是 tox 运行ning 测试使用了错误的解释器。如果相关的话,我正在使用 Windows。

找不到包,即使它已经安装 进入 venv:

C:\Users\galli\Desktop\projects\RedditBotBuilder>tox
GLOB sdist-make: C:\Users\galli\Desktop\projects\RedditBotBuilder\setup.py
py36 inst-nodeps: C:\Users\galli\Desktop\projects\RedditBotBuilder\.tox\dist\RedditBotBuilder-1.0.0.zip
py36 installed: attrs==17.4.0,certifi==2018.1.18,chardet==3.0.4,colorama==0.3.9,idna==2.6,more-itertools==4.1.0,pluggy==0.6.0,praw==5.4.0,prawcore==0.14.0,py==1.5.3,pytest==3.5.0,RedditBotBuilder==1.0.0,requests==2.18.4,six==1.11.0,update-checker==0.16,urllib3==1.22
py36 runtests: PYTHONHASHSEED='150'
py36 runtests: commands[0] | pytest --verbose tst/
============================= test session starts =============================
platform win32 -- Python 3.6.3, pytest-3.5.0, py-1.5.3, pluggy-0.6.0 -- c:\users\galli\desktop\projects\redditbotbuilder\.tox\py36\scripts\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\galli\Desktop\projects\RedditBotBuilder, inifile:
collected 0 items / 1 errors

=================================== ERRORS ====================================
_____________ ERROR collecting tst/redditbotbuilder/test_bots.py ______________
ImportError while importing test module 'C:\Users\galli\Desktop\projects\RedditBotBuilder\tst\redditbotbuilder\test_bots.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tst\redditbotbuilder\test_bots.py:3: in <module>
    from redditbotbuilder.bots import RedditBot
E   ModuleNotFoundError: No module named 'redditbotbuilder.bots'
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.22 seconds ===========================
ERROR: InvocationError: 'C:\Users\galli\Desktop\projects\RedditBotBuilder\.tox\py36\Scripts\pytest.EXE --verbose tst/'
______________________________________________________ summary _______________________________________________________
ERROR:   py36: commands failed

这是目录结构,我已将项目导入 PyCharm,它毫无怨言地解析了模块:

src/
src/redditbotbuilder/
src/redditbotbuilder/bots.py
src/redditbotbuilder/__init__.py
tst/
tst/redditbotbuilder/
tst/redditbotbuilder/test_bots.py
tst/redditbotbuilder/__init__.py
tox.ini
setup.py

这里是setup.py

setup(
    name="RedditBotBuilder",
    description="A Python framework for quickly building reddit bots",
    version="1.0.0",
    url="someUrl.com",
    author="Guy McGuyerson",
    author_email="guymcguyerson@gmail.com",
    classifiers=[
        "Programming Language :: Python",
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
        "Intended Audience :: Developers",
        "Topic :: Software Development :: Libraries :: Python Modules"
    ],
    packages=find_packages("src"),
    package_dir={'': 'src'},
    install_requires=["praw"]
)

这里是tox.ini

[tox]
envlist = py36

[testenv]
deps=pytest
commands=pytest --verbose tst/

RedditBotBuilder 已经安装在 venv:

(py36) C:\Users\galli\Desktop\projects\RedditBotBuilder\.tox\py36\Scripts>pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
attrs (17.4.0)
certifi (2018.1.18)
chardet (3.0.4)
colorama (0.3.9)
idna (2.6)
more-itertools (4.1.0)
pip (9.0.3)
pluggy (0.6.0)
praw (5.4.0)
prawcore (0.14.0)
py (1.5.3)
pytest (3.5.0)
RedditBotBuilder (1.0.0) <----------
requests (2.18.4)
setuptools (39.0.1)
six (1.11.0)
update-checker (0.16)
urllib3 (1.22)
wheel (0.30.0)

我认为可能使用了错误的解释器...

(py36) C:\Users\galli\Desktop\projects\RedditBotBuilder\.tox\py36\Scripts>where python
C:\Users\galli\Desktop\projects\RedditBotBuilder\.tox\py36\Scripts\python.exe
C:\Users\galli\AppData\Local\Programs\Python\Python36-32\python.exe

...但是在将语句 import sys; print(sys.executable) 添加到测试文件后,它打印了 C:\Users\galli\Desktop\projects\RedditBotBuilder\.tox\py36\Scripts\python.exe.

对了,我终于发现问题了。测试模块似乎隐藏了被测模块的名称。如果我将测试模块名称更改为 "notredditbotbuilder",一切正常。我不确定为什么 PyCharm 没有强调这个问题,但我不想再花时间在这上面了。