import twice when run test

2020-07-10 06:40发布

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?

1条回答
姐就是有狂的资本
2楼-- · 2020-07-10 06:54

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.

查看更多
登录 后发表回答