【发布时间】:2011-11-23 14:58:02
【问题描述】:
我认为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 文件?
更新
在 cmets 中,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
$
希望这些附加信息会有所帮助。
【问题讨论】:
-
听起来你可能遇到了 Python 包系统的副作用。
-
如果你包含 init.py 文件并且只说 ... $ py.test 会发生什么
-
看起来在...的底部可能会提示您的问题pytest.org/latest/goodpractises.html#package-name
-
值得一提的是,我在 1.3.4 版本中没有看到这种行为。不过,我已经更新到 2.2.0 版试一试,得到了相同的错误输出。
-
父目录(包含 init.py 的目录)的名称是什么?在包含代码的目录中是否还有更多的 init.py 文件?
标签: python unit-testing pytest