使用 py.test,在不同目录中调用相同的两个测试会导致 py.test 失败。这是为什么?如何在不重命名所有测试的情况下更改它?
复制做:
; 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 ============================
原文由 Sardathrion - against SE abuse 发布,翻译遵循 CC BY-SA 4.0 许可协议
放置
__init__.py
是解决冲突的一种方法。与 nose 不同,当前的 pytest 不会尝试卸载测试模块以导入具有相同导入名称的测试模块。我曾经认为这种自动取消导入有点神奇,可能会扰乱人们对导入机制所做的事情的期望;有时人们依赖测试模块的全局状态,而自动卸载会丢失它(从另一个测试模块导入的测试模块可能会做意想不到的事情)。但也许这不是一个实际问题,因此 pytest 可以添加类似的 hack …