当不同目录中的测试称为相同时,测试发现失败 [英] Test discovery failure when tests in different directories are called the same

查看:394
本文介绍了当不同目录中的测试称为相同时,测试发现失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用py.test,在不同目录中调用两个相同的测试会导致py.test失败.这是为什么?如何在不重命名所有测试的情况下更改此设置?

Using py.test, two tests called the same in different directory causes py.test to fail. Why is that? How can I change this without renaming all the tests?

要重复做:

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

Putting an __init__.py is one way of resolving the conflict. Unlike nose, current pytest does not try to unload test modules in order to import test modules with the same import name. I used to think it's a bit magic to do this auto-unimporting and might mess up people's expectation from what the import mechanism does; sometimes people rely on the global state of a test module and with auto-unloading you lose it (a test module importing from another test module might then do unexpected things). But maybe it's not a practical issue and thus pytest could add a similar hack ...

这篇关于当不同目录中的测试称为相同时,测试发现失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆