Sar*_*ica 61 python unit-testing autodiscovery pytest
使用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 ============================
Run Code Online (Sandbox Code Playgroud)
hpk*_*k42 48
提出__init__.py一个解决冲突的方法.与鼻子不同,当前pytest不会尝试卸载测试模块以导入具有相同导入名称的测试模块.我曾经认为做这种自动不进行操作有点神奇,可能会破坏人们对进口机制的期望; 有时人们依赖于测试模块的全局状态,并且自动卸载会丢失它(从另一个测试模块导入的测试模块可能会做出意想不到的事情).但也许这不是一个实际问题,因此pytest可以添加类似的黑客......
fam*_*kin 23
这是py.test的一个实际特征.你可以在pytest.org中找到这种行为的原因- 良好的集成实践 - 选择测试布局/导入规则:
- 避免
__init__.py测试目录中的文件.这样,您的测试可以轻松地针对已安装的版本运行mypkg,而不管安装的软件包是否包含测试.
因为这是使用py.test的推荐工作流程:安装正在开发的包pip install -e,然后测试它.
因此,我自己选择独特的测试名称,在约定的配置方式.它还确保您不会在各种测试运行输出中获得模糊的测试名称.
如果你需要保留测试名称并且不关心上面提到的功能,那么你可以放一个__init__.py.
我有同样的错误,但是解决方案与初始化文件或测试文件上的名称无关。我的Macbook和Docker容器上有不同的python版本。我在项目根目录的macbook的bash中而不是容器的bash中启动了一次测试。
解决方案是通过运行(从容器的bash中)删除错误创建的文件:
find -name '*.pyc' -delete
find -name __pycache__ -delete
Run Code Online (Sandbox Code Playgroud)
然后再次启动测试(仍从容器的bash进行),一切正常:
py.test
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24284 次 |
| 最近记录: |