7

从某种意义上说,我认为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 
$ 

希望这些额外的信息会有所帮助。

4

3 回答 3

16

看起来 py.test 正在py._path.pyimport用来打开您的文件。如果__init__.py目录中有文件,它将您的文件视为模块,否则打开文件。长话短说,删除__init__.py或将您的测试放在项目代码之外的另一个目录中(<--- 好主意)。

https://py.readthedocs.io/en/latest/path.html#py._path.local.LocalPath.pyimport

于 2011-12-09T19:45:22.627 回答
5

我真的建议您将目录重命名为不称为“distutils”的名称。为什么 ?因为您正在覆盖现有模块。当“import distutils”或“from distutils import *”出现在脚本中(来自另一个导入或您自己的python文件)时,它将更喜欢您的目录而不是系统目录。如果模块 distutils 之前已经加载过,则不会加载您的 distutils,因为该符号已经存在于 global() 中。

重命名该目录(如测试)而不是尝试使用 py.text / python 内部结构会更简单。

于 2011-12-06T13:42:51.367 回答
4

您可以告诉 pytest 忽略特定文件或 glob 模式,如此所述。在项目的根目录中放置一个conftest.py文件,其中列出了您要pytest忽略的文件:

然而,许多项目会有一个他们不想被导入的 setup.py。此外,可能存在只能由特定 python 版本导入的文件。对于这种情况,您可以通过在 conftest.py 文件中列出文件来动态定义要忽略的文件:

 # content of conftest.py
 import sys

 collect_ignore = ["setup.py"]
于 2014-03-25T04:13:36.517 回答