The sentinel object provides a convenient way of providing unique objects for your tests.

Attributes are created on demand when you access them by name. Accessing the same attribute will always return the same object. The objects returned have a sensible repr so that test failure messages are readable.

The DEFAULT object is a pre-created sentinel (actually sentinel.DEFAULT). It can be used by Mock.side_effect functions to indicate that the normal return value should be used.

Sentinel Example

Sometimes when testing you need to test that a specific object is passed as an argument to another method, or returned. It can be common to create named sentinel objects to test this. sentinel provides a convenient way of creating and testing the identity of objects like this.

In this example we monkey patch method to return sentinel.return_value. We want to test that this is the value returned when we call something:

>>> real = ProductionClass()
>>> real.method = Mock()
>>> real.method.return_value = sentinel.return_value
>>> returned = real.something()
>>> self.assertEquals(returned, sentinel.return_value, "something returned the wrong value")

>>> sentinel.return_value
<SentinelObject "return_value">

Table Of Contents

Previous topic

Patch Decorators

Next topic

Getting Started with Mock

This Page