[TIP] Mock multiple subprocess.Popen calls from a central point

George Monday crack.pop.cloud at gmail.com
Thu Jun 5 08:46:13 PDT 2014


Hello everyone,

I hope the subject is not too obscure.
The problem is this:

I want to write unittests for a package. The structure:

I have a toplevel module, with a class, which calls a class in another
module, that will then call a third class in a third module. There I'll
make use of subprocess.

top.class.first_method() -> next.class.dispatch_method() ->
last.class.method_using_subprocess()

The structure is necessary as it is. The problem here is, that the last
method itself will call helper
methods within its class, which all will execute different subprocess.Popen
calls, before it finally returns.

Mocking subprocess.Popen is easy with path.object etc..
But in this case, I want to unittest the first method! As long as there was
only one
subproccess.Popen call to the last method, it all worked well. But now I'm
having trouble
finding a way of being able, at the toplevel, to tell a mocked subprocess
down the road,
that it needs to provide different return values/stdout objects, as these
depend on the
command executed. The commands are defined at the lowest level. I was
thinking side_effect
with a custom method, but that gives me

side_effects_popen() takes exactly 1 argument (0 given)

And I'm not sure if this is solely because of the position of the
side_effect attribute to the mock
object or if this is the wrong approach.

Any help is much appreciated,
George
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20140605/9147e2a8/attachment.htm>


More information about the testing-in-python mailing list