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 さんはコメントで、親ディレクトリの名前は?と尋ねました。そして、特定の親ディレクトリ名 (たとえば、インストールされているパッケージの 1 つと同じ名前) しか持たない上記のエラーを再現できることがわかりました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._path.pyimportファイルを開くためにpy.test を使用しているようです。ディレクトリにファイルがある場合は、__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 が既にロードされている場合、シンボルが global() に既に存在するため、distutils はロードされません。

py.text / python internals と戦おうとするのではなく、(tests のように) そのディレクトリの名前を変更する方が本当に簡単です。

于 2011-12-06T13:42:51.367 に答える