簡體   English   中英

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

[英]`py.test` and `__init__.py` files

我認為py.test是“獨立的”,因為它“按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

我真的建議您將目錄重命名為不稱為“distutils”的名稱。 為什么 ? 因為您正在覆蓋現有模塊。 當“import distutils”或“from distutils import *”出現在腳本中時(來自另一個導入或你自己的python文件),它會更喜歡你的目錄而不是系統目錄。 如果之前已經加載了模塊 distutils,則不會加載您的 distutils,因為該符號已存在於 global() 中。

重命名該目錄(如測試)而不是嘗試使用 py.text / python 內部結構會更簡單。

你可以告訴pytest描述忽略特定文件或glob模式在這里 conftest.py文件放在項目的根目錄中,該文件列出了您希望pytest忽略的文件:

但是,許多項目都有一個他們不想導入的 setup.py。 此外,可能存在只能由特定 python 版本導入的文件。 對於這種情況,您可以通過在 conftest.py 文件中列出它們來動態定義要忽略的文件:

 # content of conftest.py import sys collect_ignore = ["setup.py"]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM