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 tests.py file for the app. Then run using:

python manage.py test my_app.TestMyCode
Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s