我原以为py.test
在某种意义上是“独立的”,它会将test_*.py
文件“按原样”处理,并且只引入这些文件中指定的模块,而不考虑任何周围的文件。但看起来我错了。下面是我与py.test
的对话:
$ ls
__init__.py test_pytest.py
$ cat __init__.py
$ cat test_pytest.py
def test_pytest():
assert True
$ py.test test_pytest.py
========================================================= test session starts ==========================================================
platform darwin -- Python 2.7.2 -- pytest-2.1.3
collected 0 items / 1 errors
================================================================ ERRORS ================================================================
___________________________________________________ ERROR collecting test_pytest.py ____________________________________________________
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/py-1.4.5-py2.7.egg/py/_path/local.py:529: in pyimport
> mod = __import__(modname, None, None, ['__doc__'])
E ImportError: No module named test_pytest
======================================================= 1 error in 0.01 seconds ========================================================
$ rm __init__.py
$ py.test test_pytest.py
========================================================= test session starts ==========================================================
platform darwin -- Python 2.7.2 -- pytest-2.1.3
collected 1 items
test_pytest.py .
======================================================= 1 passed in 0.01 seconds =======================================================
$
如何让py.test
正常工作并仍然拥有__init__.py
文件?
更新
在评论中,Holger Krekel问道,父目录的名称是什么。结果发现只有在具有特定父目录名称时(例如与已安装的包之一的名称相同,如distutils
),才能复现上述错误。请参见此处:
~/test_min
$ tree
.
└── distutils
├── __init__.py
└── test_pytest.py
1 directory, 2 files
~/test_min
$ cat distutils/__init__.py
~/test_min
$ cat distutils/test_pytest.py
def test_pytest():
assert True
~/test_min
$ py.test distutils/test_pytest.py
======================== test session starts =========================
platform darwin -- Python 2.7.2 -- pytest-2.1.3
collected 0 items / 1 errors
=============================== ERRORS ===============================
_____________ ERROR collecting distutils/test_pytest.py ______________
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/py-1.4.5-py2.7.egg/py/_path/local.py:529: in pyimport
> mod = __import__(modname, None, None, ['__doc__'])
E ImportError: No module named test_pytest
====================== 1 error in 0.01 seconds =======================
~/test_min
$ rm distutils/__init__.py
~/test_min
$ py.test distutils/test_pytest.py
======================== test session starts =========================
platform darwin -- Python 2.7.2 -- pytest-2.1.3
collected 1 items
distutils/test_pytest.py .
====================== 1 passed in 0.01 seconds ======================
~/test_min
$ touch __init__.py
~/test_min
$ ls
__init__.py distutils
~/test_min
$ touch distutils/__init__.py
~/test_min
$ py.test distutils/test_pytest.py
======================== test session starts =========================
platform darwin -- Python 2.7.2 -- pytest-2.1.3
collected 1 items
distutils/test_pytest.py .
====================== 1 passed in 0.02 seconds ======================
~/test_min
$ rm __init__.py
~/test_min
$ py.test distutils/test_pytest.py
======================== test session starts =========================
platform darwin -- Python 2.7.2 -- pytest-2.1.3
collected 0 items / 1 errors
=============================== ERRORS ===============================
_____________ ERROR collecting distutils/test_pytest.py ______________
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/py-1.4.5-py2.7.egg/py/_path/local.py:529: in pyimport
> mod = __import__(modname, None, None, ['__doc__'])
E ImportError: No module named test_pytest
====================== 1 error in 0.01 seconds =======================
~/test_min
$ mv distutils foobar
~/test_min
$ py.test foobar/test_pytest.py
======================== test session starts =========================
platform darwin -- Python 2.7.2 -- pytest-2.1.3
collected 1 items
foobar/test_pytest.py .
====================== 1 passed in 0.01 seconds ======================
~/test_min
$
希望这些额外的信息能帮到您。