File: commands.rst

package info (click to toggle)
testpath 0.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 200 kB
  • ctags: 131
  • sloc: python: 475; makefile: 156
file content (28 lines) | stat: -rw-r--r-- 1,068 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Mocking system commands
=======================

Mocking is a technique to replace parts of a system with interfaces that don't
do anything, but which your tests can check whether and how they were called.
The :mod:`unittest.mock` module in Python 3 lets you mock Python functions and
classes. These tools let you mock external commands.

Commands are mocked by creating a real file in a temporary directory which is
added to the :envvar:`PATH` environment variable, not by replacing Python
functions. So if you mock ``foo``, and your Python code runs a shell script
which calls ``foo``, it will be the mocked command that it runs.

By default, mocked commands record each call made to them, so that your test can
check these. Using the :class:`MockCommand` API, you can mock a command to do
something else.

.. note::

   These tools work by changing global state. They're not safe to use if
   commands may be called from multiple threads or coroutines.

.. currentmodule:: testpath

.. autofunction:: assert_calls

.. autoclass:: MockCommand
   :members: get_calls