Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Testing with Django Models and a lot of relations involved

Or, "How to design your Database Schema for easy Unit testing?"

By the way, there is a very similar question to this here: How to test Models in Django with Foreign Keys

I'm trying to follow TDD methodology for a project that uses the framework Django. I'm creating and testing models and its functionality (save methods, signals,...) and other high level functions that relies on the models.

I understand that unit testing must be as isolated as possible but I find myself creating a lot of tables and relations using FactoryBoy for each test, so my test is not strong enough because if something changes in a model many tests could be broken.

How to avoid all these dependencies and make the test cleaner?

What do you guys recommend to avoid all that boilerplate before the actual test?

What are the best practices?

like image 499
javier Avatar asked Aug 08 '12 22:08

javier


2 Answers

Try to use Mixer. It's much easier than 'factory_boy' and is much more powerful. You don't need to setup factories and you get data when you need them:

from mixer.backend.django import mixer

mixer.blend(MyModel)
like image 38
klen Avatar answered Sep 17 '22 12:09

klen


There is no list of best practices for testing, it's a lot of what works for you and the particular project you're working on. I agree with pyriku when he says:

You should not design your software basing on how you want to test it

But, I would add that if you have a good and modular software design, it should be easy to test properly.

I've been recently a little into unit testing in my job, and I've found some interesting and useful tools in Python, FactoryBoy is one of those tools, instead of preparing a lot of objects in the setUp() method of your test class, you can just define a factory for each model and generate them in bulk if needed.

You can also try Mocker, it's a library to mock objects and, since in Python everything is an object, you can mock functions too, it is useful if you need a test a function that generates X event at a certain time of the day, for example, send a message at 10:00am, you write a mock of datetime.datetime.now() that always returns '10:00am' and call that function with that mock.

If you also need to test some front-end or your test needs some human interaction (like when doing OAuth against ), you have those forms filled and submitted by using Selenium.

In your case, to prepare objects with relations with FactoryBoy, you can try to overwrite the Factory._prepare() method, let's do it with this simple django model:

class Group(models.Model):
    name = models.CharField(max_length=128)
    members = models.ManyToManyField(User, blank=True, null=True)

    # ...

Now, let's define a simple UserFactory:

class UserFactory(factory.Factory):
    FACTORY_FOR = User

    first_name = 'Foo'
    last_name = factory.Sequence(lambda n: 'Bar%s' % n)
    username = factory.LazzyAttribute(lambda obj: '%s.%s' % (obj.first_name, obj.last_name))

Now, let's say that I want or need that my factory generates groups with 5 members, the GroupFactory should look like this

class GroupFactory(factory.Factory):
    FACTORY_FOR = Group

    name = factory.Sequence(lambda n: 'Test Group %s' % n)

    @classmethod
    def _prepare(cls, create, **kwargs):
        group = super(GroupFactory, cls)._prepare(create, **kwargs)
        for _ in range(5):
            group.members.add(UserFactory())
        return group

Hope this helps, or at least gave you a light. Here I'll leave some links to resources related with the tools I mentioned:

Factory Boy: https://github.com/rbarrois/factory_boy

Mocker: http://niemeyer.net/mocker

Selenium: http://selenium-python.readthedocs.org/en/latest/index.html

And another useful thread about testing:

What are the best practices for testing "different layers" in Django?

like image 63
iferminm Avatar answered Sep 19 '22 12:09

iferminm