Tests coded using unittest,py.test, nose etc.
(For more advanced examples of CaptureMock usage in this environment, please see
Marc Abramowitz's examples page
Usage of capturemock for individual tests is defined by using the "@capturemock" decorator.
from capturemock import capturemock
Choosing whether to record or replay the tests can be done in one of two ways. It defaults to replay, which will of course do nothing if no previously recorded information exists. Either the environment variable CAPTUREMOCK_MODE can be set externally (to 1 for straight record, and 2 for recording only things that can't be found in the replay file), or it can be done directly in the code, like this:
from capturemock import capturemock, RECORD
Information will be recorded to a file named after the test in a subdirectory called "capturemock". In this case it would be called
For more advanced interception it's necessary to use the CaptureMock rc files
. The usual way to do that is to provide a file named ".capturemockrc" in the test directory, which will be read if nothing else overrides it. That file can then specify what is being recorded, and you can just mark the methods like this:
An rc file can also be provided as an argument to the "capturemock" decorator.
There is an additional function "set_defaults" which can be used to avoid repetition when there are multiple capturemock tests:
from capturemock import capturemock, set_defaults
If CaptureMock is running in replay mode and detects that different calls have been made, it will raise a CaptureMockReplayError exception. This includes a message suggesting that you may wish to re-record the stored mock file.
The newly created "replay" file will be saved in the "capturemock" directory as <test_name>.mock.tmp, so you can also compare it with the saved mock, and if the changes are trivial and correct, use it overwrite the saved mock also. It uses a best-fit algorithm to try and match up the responses with the closest matching call, so it may manage to do the right thing even when the calls have changed.
On occasions you want a tighter control and a stricter failure set up, in which case the best-fit algorithm may not be what you want. You can disable it by doing as follows in your rc file: