py.testを使用すると、異なるディレクトリで同じものを呼び出す2つのテストでpy.testが失敗します。何故ですか?すべてのテストの名前を変更せずにこれをどのように変更できますか?複製するpy.test - 異なるディレクトリのテストが同じと呼ばれるときのテストディスカバリの失敗
は行います__init__.py
を置く
; cd /var/tmp/my_test_module
; mkdir -p ook/test
; mkdir -p eek/test
; touch ook/test/test_proxy.py
; touch eek/test/test_proxy.py
; py.test
============================= test session starts ==============================
platform linux2 -- Python 2.7.3 -- pytest-2.2.4
collected 0 items/1 errors
==================================== ERRORS ====================================
___________________ ERROR collecting ook/test/test_proxy.py ____________________
import file mismatch:
imported module 'test_proxy' has this __file__ attribute:
/home/ygolanski/code/junk/python/mymodule/eek/test/test_proxy.py
which is not the same as the test file we want to collect:
/home/ygolanski/code/junk/python/mymodule/ook/test/test_proxy.py
HINT: remove __pycache__/.pyc files and/or use a unique basename for your test file modules
=========================== 1 error in 0.01 seconds ============================
__init__.pyを必要とすることは意味があると私は同意します。テストがパッケージに含まれていない場合は、本質的にトップレベルのモジュール(OPのtest_proxy)であり、テストは1つだけでなければなりません。テストモジュールを適切なパッケージ(ookとeek)に入れることによって、テストの適切な名前空間を提供します。現状は最高だと言います。この質問へのエラーメッセージのリンクや、問題を回避するための推論とテクニックを説明するドキュメントの何かに、痛みを緩和するかもしれません。 –
py.test docsは、テストディレクトリに '__init __。py'を置くことを特に勧めています:_" __init __。py'ファイルをテストディレクトリに置かないでください。この方法は、インストールされたバージョンのmypkgに対して簡単に実行できます。インストールされたパッケージにテストが含まれているかどうかとは無関係に "_。 [pytest.org - Good Integration Practices](http://pytest.org/latest/goodpractises.html#choosing-a-test-layout-import-rules)を参考にしてください。 – famousgarkin
アップデート:上記の@ famousgarkinのコメントとanswer(https://stackoverflow.com/a/21942491/260303)の推奨事項は、もはやドキュメントには表示されません(少なくとも「回避」を検索すると見積もりが表示されません上記):https://docs.pytest.org/en/latest/goodpractices.html#tests-as-part-of-application-code実際、そのリンクの例では、テストディレクトリに '__init __。py'と表示されているので、受け入れられた答えが正しいと思われます。 –