Arun Sasi
Arun Sasi

Reputation: 336

Import Error while running pytest in virtualenv

I am trying to run my pytest (bdd) test cases in virtualenv. I have created a requirements.txt (using pip freeze) file in the root folder as below.

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

I have created the virtual env, activated the same and installed the dependencies using the below commands.

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

However when I am trying to run the test cases, am getting the below errors.

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)

Can you help me understand what I am missing here? I tried with different versions of pytest, without luck. However the test cases are running fine in my local without any issues.

Upvotes: 3

Views: 3465

Answers (1)

hoefling
hoefling

Reputation: 66171

There's an open issue with pytest-yield that prevents it to work with latest pytest version (5.1 and up): #6. This means that you have either to downgrade to an older version of pytest:

$ pip install "pytest<5"

Or uninstall pytest-yield until the above issue is resolved:

$ pip uninstall -y pytest-yield

Upvotes: 2

Related Questions