gpt4 book ai didi

python - `py.test` 和 `__init__.py` 文件

转载 作者:太空狗 更新时间:2023-10-29 22:03:48 26 4
gpt4 key购买 nike

我认为 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
$

希望这些附加信息能有所帮助。

最佳答案

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

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

关于python - `py.test` 和 `__init__.py` 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8244277/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com