[TIP] How would you test something that deals with processes?

Alfredo Deza alfredodeza at gmail.com
Mon Jun 6 17:53:11 PDT 2011

On Mon, Jun 6, 2011 at 8:25 PM, John Anderson <sontek at gmail.com> wrote:

> I wrote this script that monitors memory usage of processes and kills them
> if they get too high.  I'm having a hard time figuring out how to test
> something like this.

I've had similar issues when testing deamons... it is hard if what you are
trying to test is double forking!

> Would you just mock out the process list and test that if you pass it a
> process list it calls send_signal?  Thats what i'm thinking about doing but
> would like to hear other peoples opinions!

There are a couple of things I would do, but most definitely I would start
by breaking your script in such a way that the actual call to the processes
is a
simple/small function (maybe a method).

If you get to that point, then you get to test all the other parts easily,
without worrying about the process list.

Having said that, you still need to test the *actual* work that needs to be
done to get information about the process. In that case I would mock the
processes but I would keep a couple of separate tests that do not actually
use mock at all but deal with the *real* thing.

You could create real processes with certain memory parameters and then
assert that your script is killing them appropriately. That way you end up
the best of both worlds... you are testing all the "unit" parts of your
script, but as a safety net, you are also doing some testing with actual

I would run the unit tests that use mocking all the time and would save
myself to run the non-mocking ones at other moments (e.g. when getting close
to a new library version or release).

> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20110606/ba4e66cc/attachment.htm>

More information about the testing-in-python mailing list