root
| +-- demo
|-->__init__.py
|-->conftest.py
|-->test.py
conftest.py
import pytest
def tear_down():
print "\nTEARDOWN after all tests"
@pytest.fixture(autouse=True)
def set_up(request):
print "\nSETUP before all tests"
if request.cls.__name__ == 'TestClassA':
return ["username", "password"]
request.addfinalizer(tear_down)
test.py
#import requests # this is commented
class TestClassA:
def test_1(self,set_up):
print "test A1 called"
print("username :-- %s and password is %s" % (set_up[0], set_up[1]))
def test_2(self):
print "test A2 called"
class TestClassB:
def test_1(self):
print "test B1 called"
pytest -s -v demo/test.py::TestClassA
This code works fine. observe the first line of test.py
, it's commented. Now, if I run the same script with uncommenting import requests
, getting below error
ImportError while importing test module 'some_path/../demo/test.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/local/lib/python2.7/site-packages/six-1.11.0-py2.7.egg/six.py:709: in exec_
exec("""exec _code_ in _globs_, _locs_""")
demo/test.py:1: in <module>
import requests
E ImportError: No module named requests
Executing without pytest, works fine (no import error)
And also, if test.py
calls the function of other module (which has import requests
) throws same error. Is it conflict of request of pytest
? I really don't understand this, can you please help me ?
which python : /Library/Frameworks/Python.framework/Versions/2.7/bin/python
pytest --version : 'imported from /usr/local/lib/python2.7/site-packages/pytest-3.8.2-py2.7.egg/pytest.pyc'
, is this a reason for failure ?
pytest --version
This is pytest version 3.8.2, imported from /usr/local/lib/python2.7/site-packages/pytest-3.8.2-py2.7.egg/pytest.pyc
setuptools registered plugins:
celery-4.0.2 at /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/celery/contrib/pytest.py
hypothesis-3.8.2 at /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/hypothesis/extra/pytestplugin.pyc
pytest-cov-2.4.0 at /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pytest_cov/plugin.py