我有以下目录结构
/home/ubuntu/test/
- Foo/
- Foo.py
- __init__.py
- Test/
- conftest.py
- __init__.py
- Foo/
- test_Foo.py
- __init__.py
Run Code Online (Sandbox Code Playgroud)
Foo.py 包含
class Foo(object):
def __init__(self):
pass
Run Code Online (Sandbox Code Playgroud)
conftest.py 包含:
import pytest
import sys
print sys.path
from Foo.Foo import Foo
@pytest.fixture(scope="session")
def foo():
return Foo()
Run Code Online (Sandbox Code Playgroud)
test_Foo.py 包含:
class TestFoo():
def test___init__(self,foo):
assert True
Run Code Online (Sandbox Code Playgroud)
如果我运行 pytest 。在 Test 文件夹中,然后我收到一个错误,它找不到模块 Foo:
Traceback (most recent call last):
File "/home/ubuntu/pythonVirtualEnv/local/lib/python2.7/site-packages/_pytest/config.py", line 379, in _importconftest
mod = conftestpath.pyimport()
File "/home/ubuntu/pythonVirtualEnv/local/lib/python2.7/site-packages/py/_path/local.py", line 662, in pyimport
__import__(modname)
File "/home/ubuntu/pythonVirtualEnv/local/lib/python2.7/site-packages/_pytest/assertion/rewrite.py", line 212, in load_module
py.builtin.exec_(co, mod.__dict__)
File "/home/ubuntu/pythonVirtualEnv/local/lib/python2.7/site-packages/py/_builtin.py", line 221, in exec_
exec2(obj, globals, locals)
File "<string>", line 7, in exec2
File "/home/ubuntu/test/Test/conftest.py", line 6, in <module>
from Foo.Foo import Foo
ImportError: No module named Foo
ERROR: could not load /home/ubuntu/test/Test/conftest.py
Run Code Online (Sandbox Code Playgroud)
打印在 conftest.py 中的 sys.path 似乎包含 /home/ubuntu/test 路径,所以它应该能够找到 Foo.py,对吧?
问题是它仅在我将 conftest.py 移动到下面的文件夹时才有效。
我运行 pytest 3.2.2
小智 7
该错误表示conftest.py无法加载,因为ImportError. 尝试在 foo 夹具中移动您的导入,如下所示:
import pytest
import sys
print sys.path
@pytest.fixture(scope="session")
def foo():
from Foo.Foo import Foo
return Foo()
Run Code Online (Sandbox Code Playgroud)