case

Python unittest Utilities

class case.Case(methodName='runTest')[source]

Test Case

Subclass of unittest.TestCase adding convenience methods.

setup / teardown

New setup() and teardown() methods can be defined in addition to the core setUp() + tearDown() methods.

Note: If you redefine the core setUp() + tearDown()
methods you must make sure super is called. super is not necessary for the lowercase versions.

Python 2.6 compatibility

This class also implements assertWarns(), assertWarnsRegex(), assertDictContainsSubset(), and assertItemsEqual() which are not available in the original Python 2.6 unittest implementation.

exception DeprecationWarning

Base class for warnings about deprecated features.

exception Case.PendingDeprecationWarning

Base class for warnings about features which will be deprecated in the future.

Case.assertDeprecated(*args, **kwds)[source]
Case.assertDictContainsSubset(expected, actual, msg=None)[source]
Case.assertItemsEqual(expected_seq, actual_seq, msg=None)[source]
Case.assertPendingDeprecation(*args, **kwds)[source]
Case.assertWarns(expected_warning)[source]
Case.assertWarnsRegex(expected_warning, expected_regex)[source]
Case.setUp()[source]
Case.setup()[source]
Case.tearDown()[source]
Case.teardown()[source]
case.ContextMock(*args, **kwargs)[source]

Mock that mocks with statement contexts.

class case.MagicMock(*args, **kwargs)[source]
class case.Mock(*args, **kwargs)[source]
case.call

A tuple for holding the results of a call to a mock, either in the form (args, kwargs) or (name, args, kwargs).

If args or kwargs are empty then a call tuple will compare equal to a tuple without those values. This makes comparisons less verbose:

_Call(('name', (), {})) == ('name',)
_Call(('name', (1,), {})) == ('name', (1,))
_Call(((), {'a': 'b'})) == ({'a': 'b'},)

The _Call object provides a useful shortcut for comparing with call:

_Call(((1, 2), {'a': 3})) == call(1, 2, a=3)
_Call(('foo', (1, 2), {'a': 3})) == call.foo(1, 2, a=3)

If the _Call has no name then it will match any name.

case.patch(target, new=sentinel.DEFAULT, spec=None, create=False, spec_set=None, autospec=None, new_callable=None, **kwargs)[source]

patch acts as a function decorator, class decorator or a context manager. Inside the body of the function or with statement, the target is patched with a new object. When the function/with statement exits the patch is undone.

If new is omitted, then the target is replaced with a MagicMock. If patch is used as a decorator and new is omitted, the created mock is passed in as an extra argument to the decorated function. If patch is used as a context manager the created mock is returned by the context manager.

target should be a string in the form ‘package.module.ClassName’. The target is imported and the specified object replaced with the new object, so the target must be importable from the environment you are calling patch from. The target is imported when the decorated function is executed, not at decoration time.

The spec and spec_set keyword arguments are passed to the MagicMock if patch is creating one for you.

In addition you can pass spec=True or spec_set=True, which causes patch to pass in the object being mocked as the spec/spec_set object.

new_callable allows you to specify a different class, or callable object, that will be called to create the new object. By default MagicMock is used.

A more powerful form of spec is autospec. If you set autospec=True then the mock with be created with a spec from the object being replaced. All attributes of the mock will also have the spec of the corresponding attribute of the object being replaced. Methods and functions being mocked will have their arguments checked and will raise a TypeError if they are called with the wrong signature. For mocks replacing a class, their return value (the ‘instance’) will have the same spec as the class.

Instead of autospec=True you can pass autospec=some_object to use an arbitrary object as the spec instead of the one being replaced.

By default patch will fail to replace attributes that don’t exist. If you pass in create=True, and the attribute doesn’t exist, patch will create the attribute for you when the patched function is called, and delete it again afterwards. This is useful for writing tests against attributes that your production code creates at runtime. It is off by by default because it can be dangerous. With it switched on you can write passing tests against APIs that don’t actually exist!

Patch can be used as a TestCase class decorator. It works by decorating each test method in the class. This reduces the boilerplate code when your test methods share a common patchings set. patch finds tests by looking for method names that start with patch.TEST_PREFIX. By default this is test, which matches the way unittest finds tests. You can specify an alternative prefix by setting patch.TEST_PREFIX.

Patch can be used as a context manager, with the with statement. Here the patching applies to the indented block after the with statement. If you use “as” then the patched object will be bound to the name after the “as”; very useful if patch is creating a mock object for you.

patch takes arbitrary keyword arguments. These will be passed to the Mock (or new_callable) on construction.

patch.dict(...), patch.multiple(...) and patch.object(...) are available for alternate use-cases.