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
Этот код работает нормально.обратите внимание на первую строку test.py
, это прокомментировано.Теперь, если я запускаю тот же сценарий с раскомментированием import requests
, получая ошибку ниже
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
Выполнение без pytest, работает нормально (без ошибки импорта) А также, если test.py
вызывает функцию другого модуля(which has import requests
) выдает ту же ошибку.Это конфликт request of pytest
?Я действительно не понимаю этого, не могли бы вы мне помочь?
какой питон: /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'
, это причина сбоя?
pytest - версия
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