Navigation: home >capturemock>filtering
Capture-Replay Mocking with CaptureMock
Filtering what is recorded
Repeated calls
Ordinarily, CaptureMock will record every call made to an intercepted function. This can be important, as some functions will return different results when called subsequently. For some things though, it just creates clutter, and then it's useful to be able to disable it.
For example, suppose we want to test locale-specific behaviour. The easiest way to do that is to intercept locale.getdefaultlocale, but the value of this is very unlikely to change during the run. So we configure as follows:
[python]
intercepts = locale.getdefaultlocale

[locale.getdefaultlocale]
check_repeated_calls = False
This will then result in a mock file that neatly says something like
<-PYT:locale.getdefaultlocale()
->RET:('en_US', 'UTF8')
instead of the same information repeated lots of times for every time we call this method.
You can also disable it "across the board", i.e. in the general Python section, like this:
[python]
intercepts = locale.getdefaultlocale,something.else,and.another.one
check_repeated_calls = False
Callstack filtering
If you intercept something quite low-level, such as "os.getpid" you run into the issue that this may well be called by the standard library itself, or by development tools like StoryText or coverage.py. This obviously leads to far more calls being intercepted and recorded than you actually want.
CaptureMock therefore makes sure that any calls made by the standard library or by its own interceptor programs are not intercepted and recorded. This does mean that you need to intercept exactly the right thing. For example it will not work to intercept "subprocess.Popen" if your program actually calls "subprocess.call", even though the latter calls the former internally.
To tell it to block calls made from other places, you would do as follows.
[python]
intercepts = os.getpid
ignore_callers = coverage,usecase
This will intercept "os.getpid" except when it is called from any module in a directory called "coverage" or from the "usecase" module, assuming our tests might use StoryText and coverage.py to extract information.


Last updated: 28 February 2020