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?