当不同目录中的测试称为相同时,测试发现失败

发布于 2021-01-29 17:04:15

使用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 ============================
关注者
0
被浏览
51
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    放置一个__init__.py是解决冲突的一种方法。与鼻子不同,当前的pytest不会尝试卸载测试模块以导入具有相同导入名称的测试模块。我以前认为执行此自动取消导入功能有点魔术,并且可能会破坏人们对导入机制的期望。有时人们依赖测试模块的全局状态,而自动卸载会使您迷失它(从另一个测试模块导入的测试模块可能会执行意外的操作)。但是也许这不是一个实际问题,因此pytest可能会添加类似的hack



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看