Testing in Python


Testing in Python

Python comes with a few unique features specifically for testing. One of those features is Doctests. Doctests let you define simple unit tests right on your method. This is especially useful for test-driven development, where you have explicit examples you need to code against. Another one of those features is Mocking. Mocking allows you to overwrite an object and control it's behavior while testing.


Can you make this doctest pass?


Mocking is Python's most powerful testing tool. A Mock Object completely overwrites the object you specify. This can be a class or a method. Mock Objects have two special fields, return_value and side_effect. Both of these fields effect the result of when a Mock is called.

For example:

Create your playground on
This playground was created on, our hands-on, knowledge-sharing platform for developers.
Go to