mockproc Package

mockproc Package

Package for the mockproc test fixture

mockprocess Module

class mockproc.mockprocess.MockProc(bindir=None)[source]

Bases: object

Provides basic path-overriding process mocks for nose (or similar) test frameworks

You would normally use a MockProc where you have code which uses subprocess to call an external program which would have negative effects were it to actually be called within your test framework.

You can (optionally) provide a “bindir” argument to the MockProc to specify where the executable scripts should be written (e.g. to avoid having the scripts written into a noexec partition).

Note: cleanup for the MockProc unconditionally removes bindir (even if you created it) when .exit() is called.

append(executable, returncode=0, stdout=None, stderr=None, script=None)[source]

Add a new script to the bin-dir, ‘pushes’ the script into the stack...

executable – the name of the executable, note that this must be a PATH-lookup executable name, not a full path to an executable

returncode – the return code to return from the executable (script)

stdout – the standard output for the process, if None, nothing will be output

stderr – the standard error for the process, if None, nothing will be output

script – allows you to specify the script to be written, e.g. to allow you to simulate a process which takes a very long time, or produces extremely large amounts of output/errors.

returns the internal description of the script (basically the parameters as a dictionary)


Write scripts to bindir (possibly creating it) and set PATH to use it


Delete bindir and remove from PATH


Pop the current executable from our set of scripts

If the MockProc is currently live, will also delete the script when the last executable of this name is removed, or write the next version of the executable into the scripts directory.

script_template = '#! /usr/bin/env python\n\n# mocked implementation of %(executable)s\n\nimport os,sys\nstdout = %(stdout)r\nstderr = %(stderr)r\nif stdout:\n sys.stdout.write( stdout )\nif stderr:\n sys.stderr.write( stderr )\nsys.exit( %(returncode)s )\n'

Table Of Contents

Previous topic

MockProc for Python, Subprocess Stubs for Tests

This Page