[TIP] Strategies for testing exec

Gregory P. Smith greg at krypto.org
Sun Dec 9 22:19:39 PST 2012

On Sun, Dec 9, 2012 at 10:06 PM, Alex Gaynor <alex.gaynor at gmail.com> wrote:

> Title pretty much says it all, I've got some code that execs, I want to
> write tests for it. So far I'm just having my tests fork, which sucks but
> works ok. I've also thought about mocking exec, but that seems brittle
> (although maybe not, exec is kind of a well defined API boundary. I'm
> wondering if anyone has any suggestions?

os.execv*() is well defined so don't feel bad if stubbing it out is what
you choose to do for simplicity.

> Alex
> PS: Motivation for moving away from fork is that py.test --looponfail
> leaks child processes, and I'm getting tired of cleaning up zombies every 5
> minutes :D

Running the code being tested within a subprocess one way or another is the
way to test that exec actually did what you asked it to... I'd also avoid
fork() itself within your test code but there is no reason you can't use
subprocess to launch a small stand alone program as your test case that the
parent monitors one way or another to figure out if it did the right thing.

Several subprocess tests in the standard library do this.


PS as always if you're doing this in Python 2.x i recommend using
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20121209/815895b0/attachment-0001.htm>

More information about the testing-in-python mailing list