Once in a while, we release projects to the community as Open Source Software under the MIT license. You can download these projects below.

Silver Mock v0.1 (Beta)

Silver Mock is a simple, lightweight, python library for unit testing code using Mock Objects. It was developed for our internal unit tests. The API is loosely based on JSMock. For some other Python mocking libraries, see PyMock and pMock.

Download

Download SilverMock (3.2 KB)

Limitations

SilverMock does not yet support keyword parameters. This will be added in the next release.

Reporting bugs and submitting patches

Send them to siddharta@silverstripesoftware.com

Installation

Untar the archive and copy SilverMock.py to your [python dir]/Lib/site-packages or some place on your Python path.

Documentation

View this introductory video for a basic overview of SilverMock (8 minutes)

A typical usage pattern is given below


# The class and method to test
class TestObject:
    def fn(self, obj):
        count = obj.count("abcd")
        return count + 1

# The test case
import unittest
from SilverMock import *

class TestCases(unittest.TestCase):
    def testFnReturnsCountPlusOne(self):
        # MockObject creates a Mock Object class
        # It takes as parameters, a name and a list of expected calls
        MockObj = MockObject("Mock obj", [
            # An expectation is created by calling ShouldBeCalled 
            # with the name of the function to be called
            ShouldBeCalled("__init__"),

            # You can check arguments by passing a tuple of expected parameters
            # to the with_args method. You can return values by passing the 
            # value to return to the and_return method
            ShouldBeCalled("count").with_args(("abcd",)).and_return(4)
        ])
        # Create an object from the mock object class we created
        obj = MockObj()
        test_object = TestObject()

        # Make the call
        return_value = test_object.fn(obj)    

        # Verify that the calls were made correctly
        obj.verify()

        # The mock object returns 4 when called, so we expect 5 here
        self.assertEquals(5, return_value)


Download

Download SilverMock (3.2 KB)