Django Testing using Mock

Two important 3rd party testing tools for Django are Mock and Mommy. Both allow you to quickly create instances of Django models for testing. The advantage of Mock is it is fast because it does not require a test database to be setup. It is used primarily for unit testing.

Here is a skeleton of a mock test:

import unittest
import mock

from models import MyModel

class TestMyCode(unittest.TestCase):
    def test_1(self):
        instance = mock.Mock(spec=MyModel)
        # Do something with instance

Put this in your file for the app. Then run using:

python test my_app.TestMyCode

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s