Faster tests
If you are new to TDD, writing the tests might be a real pain, not to mention about worrying how fast they run.
Factory Boy
If you already are familiar or just getting started with OOP and feel comfortable with actually checking the attribute in order to make it obvious that the value was changed, then Factory Boy is really a tool for you.
It is a handy tool to help you get started with writing tests and checking how objects relate to each other.
The down-side is that you will have to always create a "context" to make sure that you have all the entries you need. Eg if you are adding an article, you first need to manually create (hardcode) the creation of a category.
class ArticleTest(TestCase): def setUp(self): self.client = Client() selc.category = CategoryFactory() def test_create_article(self): data = {'title': 'Foo', 'teaser': 'Foobar', 'category': self.category.id} self.client.post(reverse('articles:add'), data) self.assertEqual(Article.objects.all().count(), 1) article = Article.objects.get(title=data['title']) self.assertTrue(article in Article.objects.all()) self.assertTrue(article.teaser == data['teaser']) serf.assertTrue(article.category == data['category'])
Mock
Mock on the other hand, is a more advanced tool, and requires a totally different approach and even a different mentality about how the tests must be written and what should be tested.
Isolation is its main advantages and you dont have to create a "context" so that your tests can run.
class ArticleTest(TestCase): def setUp(self): self.client = Client() @mock.patch('article.models.Article') def test_create_article(self, MockArticle): data = {'title': 'Foo', 'teaser': 'Foobar', 'category': 1} self.client.post(reverse('articles:add'), data) MockArticle.objects.create.assert_called_once_with(**data)
The test's goal is to make sure that if some POST data is sent, then a .create() call is made with the correct arguments. The action done by. create() is checked in another test.
Not having to deal with DB or other hooks makes this approach the faster one.


















