R – import twice when run test

djangoimporttesting

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?

Best Answer

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.