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

时间:2018-03-30 10:01:45

标签: python tox

我在使用tox时遇到了一些麻烦。测试无法运行,因为找不到测试模块。从我所做的挖掘中,我怀疑问题可能是tox正在使用错误的解释器运行测试。我正在使用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

1 个答案:

答案 0 :(得分:0)

是的,我终于发现了这个问题。测试模块似乎遮蔽了被测模块的名称。如果我将测试模块名称更改为“notredditbotbuilder”,一切正常。我不确定为什么PyCharm没有强调这个问题,但我不想再浪费时间了。