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

Martin Pool mbp at sourcefrog.net
Mon Jun 6 21:15:48 PDT 2011

On 7 June 2011 10:25, 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.
> 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!

I think in this kind of case it is good to be clear about what you're testing:

1- "I am totally sure I know how to use the OS interface correctly; I
just want to check I'm using it as I expect"

2- "I need to make sure I'm using the OS interface properly to get
memory usage, kill them, etc."

In the first case it makes sense to abstract the OS interface and
provide a testing version that does not actually need to deal with
real processes.

In the second case you probably do want to actually read real memory
usage, kill processes, etc.

Sometimes the second part of this kind of problem is too hard to
reliably test automatically, and you just need to do it manually, then
do the higher-level tests.

The nice thing about splitting them is that then you only have to test
narrow OS interfaces: "get memory usage" and "kill process", without
simulating an actually-too-big process.


More information about the testing-in-python mailing list