skip to navigation
skip to content

Funk 0.3

A mocking framework for Python

Downloads ↓ | Package Documentation

Funk is a mocking framework for Python, influenced heavily by JMock. Funk helps to test modules in isolation by allowing mock objects to be used in place of "real" objects. Funk is licensed under the 2-clause BSD licence.

Example

Let's say we have a TagRepository class, which has a fetch_all method on it. This method will fetch all instances of Tag from the database for us. The Tag class looks like this:

class Tag(object):
    def __init__(self, name):
        self.name = name

We also have a class that we'd like to test, called TagDisplayer. Its constructor takes a TagRepository, and has a method display_all. We want to test that this method will grab all of the tags from the repository, sort them into alphabetical order, and write their names into a string, separated by new lines:

from nose.tools import assert_equals
import funk
from funk import expects

@funk.with_context
def test_tag_displayer_writes_all_tag_names_in_alphabetical_order_onto_separate_lines(context):
    tag_repository = context.mock(TagRepository)
    expects(tag_repository).fetch_all(sorted=False).returns([Tag('python'), Tag('debian')])

    tag_displayer = TagDisplayer(tag_repository)
    assert_equals(tag_displayer.display_all(), 'debian\npython')

By using a mock object instead of a real instance of TagRepository, we avoid relying on a correct implementation of TagRepository. We can also run the test without a running database.

 
File Type Py Version Uploaded on Size # downloads
Funk-0.3.tar.gz (md5) Source 2010-09-22 21KB 381