I have a Django model. Among other things it has a ForeignKey
to User
:
class MyModel(models.Model):
foo = models.BooleanField()
bar = models.ForeignKey(User)
I have a method in my tests that generates me an appropriately shaped User
- generate_test_user()
I want to write some tests with Hypothesis to assert assorted properties about instances of my model.
My first implementation looked like this:
class MyTestCase(TestCase):
@hypothesis.given(models(MyModel, bar=just(generate_test_user())))
def test_my_model(self, mymodel):
pass
However this fails because generate_test_user
gets called at import time and thus tries to create a model before Django migrations etc. have run.
What's a good way to craft a strategy such that the right things get evaluated at the right times / delay evaluation of the just
call / similar?