python - 在 virtualenv 中运行 pytest 时导入错误

标签 python python-3.x unit-testing virtualenv pytest

我正在尝试在 virtualenv 中运行我的 pytest (bdd) 测试用例。我在根文件夹中创建了一个requirements.txt(使用pip freeze)文件,如下所示。

apipkg==1.5
atomicwrites==1.3.0
attrs==19.1.0
behave==1.2.6
certifi==2019.6.16
chardet==3.0.4
chromedriver==2.24.1
contextlib2==0.6.0.post1
coverage==4.5.4
docker==4.2.0
execnet==1.7.1
extras==1.0.0
Faker==4.1.1
fixtures==3.0.0
fuzzywuzzy==0.17.0
glob2==0.7
html-testRunner==1.2
html2text==2020.1.16
HTMLParser==0.0.2
idna==2.8
imaplib2==2.45.0
importlib-metadata==0.23
Jinja2==2.9.5
lettuce==0.2.23
lettuce-webdriver==0.3.5
linecache2==1.0.0
Mako==1.1.0
MarkupSafe==1.1.1
mock==3.0.5
more-itertools==7.0.0
packaging==19.2
parse==1.11.1
parse-type==0.4.2
path==15.0.0
path.py==12.5.0
pbr==5.4.2
pi==0.1.2
pipenv==2018.11.26
pluggy==0.13.0
py==1.8.0
pyparsing==2.4.2
pyperclip==1.7.0
PyQt5==5.13.0
PyQt5-sip==4.19.18
PyQtWebEngine==5.13.0
pytest==5.1.2
pytest-bdd==3.2.1
pytest-docker-fixtures==1.3.6
pytest-fixture-config==1.7.0
pytest-forked==1.1.3
pytest-html==2.0.0
pytest-metadata==1.8.0
pytest-ordering==0.6
pytest-shutil==1.7.0
pytest-splinter==2.0.1
pytest-virtualenv==1.7.0
pytest-xdist==1.31.0
pytest-yield==1.0.0
python-dateutil==2.8.1
python-mimeparse==1.6.0
python-subunit==1.3.0
PyYAML==5.3.1
QScintilla==2.11.2
requests==2.22.0
responses==0.10.9
selenium==3.141.0
six==1.12.0
splinter==0.11.0
sure==1.4.11
termcolor==1.1.0
testtools==2.3.0
text-unidecode==1.3
traceback2==1.4.0
unittest2==1.1.0
urllib3==1.24.1
virtualenv==16.7.2
virtualenv-clone==0.5.3
wcwidth==0.1.7
websocket-client==0.57.0
zipp==0.6.0

我已经创建了虚拟环境,激活了虚拟环境并使用以下命令安装了依赖项。

virtualenv test
source test/bin/activate
pip install -r requirements.txt

但是,当我尝试运行测试用例时,出现以下错误。

Traceback (most recent call last):
  File "/test/bin/pytest", line 8, in <module>
    sys.exit(main())
  File "/test/lib/python3.7/site-packages/_pytest/config/__init__.py", line 59, in main
    config = _prepareconfig(args, plugins)
  File "/test/lib/python3.7/site-packages/_pytest/config/__init__.py", line 209, in _prepareconfig
    pluginmanager=pluginmanager, args=args
  File "/test/lib/python3.7/site-packages/pluggy/hooks.py", line 286, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "/test/lib/python3.7/site-packages/pluggy/manager.py", line 92, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/test/lib/python3.7/site-packages/pluggy/manager.py", line 86, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "/test/lib/python3.7/site-packages/pluggy/callers.py", line 203, in _multicall
    gen.send(outcome)
  File "/test/lib/python3.7/site-packages/_pytest/helpconfig.py", line 89, in pytest_cmdline_parse
    config = outcome.get_result()
  File "/test/lib/python3.7/site-packages/pluggy/callers.py", line 80, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/test/lib/python3.7/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "/test/lib/python3.7/site-packages/_pytest/config/__init__.py", line 720, in pytest_cmdline_parse
    self.parse(args)
  File "/test/lib/python3.7/site-packages/_pytest/config/__init__.py", line 928, in parse
    self._preparse(args, addopts=addopts)
  File "/test/lib/python3.7/site-packages/_pytest/config/__init__.py", line 874, in _preparse
    self.pluginmanager.load_setuptools_entrypoints("pytest11")
  File "/test/lib/python3.7/site-packages/pluggy/manager.py", line 297, in load_setuptools_entrypoints
    plugin = ep.load()
  File "/test/lib/python3.7/site-packages/importlib_metadata/__init__.py", line 92, in load
    module = import_module(match.group('module'))
  File "/test/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "/test/lib/python3.7/site-packages/_pytest/assertion/rewrite.py", line 140, in exec_module
    exec(co, module.__dict__)
  File "/test/lib/python3.7/site-packages/pytest_yield/plugin.py", line 11, in <module>
    from _pytest.python import Generator
ImportError: cannot import name 'Generator' from '_pytest.python' (/test/lib/python3.7/site-packages/_pytest/python.py)

你能帮我理解我在这里缺少什么吗?我尝试了不同版本的 pytest,但没有运气。然而,测试用例在我的本地运行良好,没有任何问题。

最佳答案

pytest-yield 存在一个 Unresolved 问题,导致其无法与最新的 pytest 版本(5.1 及更高版本)一起使用:#6 。这意味着您必须降级到旧版本的 pytest:

$ pip install "pytest<5"

或者卸载pytest-yield直到上述问题解决:

$ pip uninstall -y pytest-yield

关于python - 在 virtualenv 中运行 pytest 时导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63128862/

相关文章:

python - 颜色编码图 Python

python - 类似pandas语句的SQL where子句

ios - 如何测试使用 UIKit 的 iOS 库

python - 使用 Python 将 YouTube 视频下载到某个目录

python - 过滤 Pandas Dataframe 中不同的列

ios - 如何在不暴露私有(private)函数的情况下在单元测试中操作核心数据?

c# - 如何查找包含 nunit 测试的 dll 文件

python - 使用循环创建 Pandas Dataframe

python - 自动增量主键和链接表(Mysql)

python - 路径库.py : Instantiating 'PosixPath' on Windows