运行测试时导入两次 [英] import twice when run test

查看:98
本文介绍了运行测试时导入两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的tests.py中有这个代码:

 从模型导入* 

并且在models.py中我有一个信号处理程序并将其注册到

  post_save.connect(post_save_note,sender =注)

我运行测试与 ./ manage.py test main



我发现信号处理程序已注册两次并执行两次,
,我发现这是因为模型被导入两次。



不能从模型导入* $ c>在测试代码中?
我应该怎么做?

解决方案

你需要确保你的模型总是以相同的方式导入。 p>

所以例如,如果你在 example1 / tests.py 中:

 从模型导入* 

和另一个package example2 / views.py

  from example1.models import ModelA 

那么你会遇到你遇到的问题。解决方案是一致的,只能使用一个或另一个。


i have this code in my tests.py:

from models import *

and in the models.py I have a signal handler and register it with

post_save.connect( post_save_note, sender=Note )

and when i run test with ./manage.py test main

I found the signal handler was registered twice and executed twice, and I found it's because the models was imported twice.

can't i put from models import * in the test code? what should i do?

解决方案

You need to make sure your models are always imported the same way.

So for example, if you have in example1/tests.py:

from models import *

and in another package example2/views.py:

from example1.models import ModelA

then you'll have the problem you are experiencing. The solution is to be consistent and use only one or the other.

这篇关于运行测试时导入两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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