Note that there are some explanatory texts on larger screens.

plurals
  1. POPython Mock Process for Unit Testing
    text
    copied!<p><strong>Background:</strong><br> I am currently writing a process monitoring tool (Windows and Linux) in Python and implementing unit test coverage. The process monitor hooks into the Windows API function <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms682629(v=vs.85).aspx" rel="nofollow noreferrer">EnumProcesses</a> on Windows and monitors the /proc directory on Linux to find current processes. The process names and process IDs are then written to a log which is accessible to the unit tests.</p> <p><strong>Question:</strong><br> When I unit test the monitoring behavior I need a process to start and terminate. I would love if there would be a (cross-platform?) way to start and terminate a fake system process that I could uniquely name (and track its creation in a unit test).</p> <p><strong>Initial ideas:</strong><br></p> <ul> <li>I could use <a href="http://docs.python.org/2/library/subprocess.html#popen-objects" rel="nofollow noreferrer">subprocess.Popen()</a> to open any system process but this runs into some issues. The unit tests could falsely pass if the process I'm using to test is run by the system as well. Also, the unit tests are run from the command line and any Linux process I can think of suspends the terminal (nano, etc.).</li> <li>I could start a process and track it by its process ID but I'm not exactly sure how to do this without suspending the terminal.</li> </ul> <p>These are just thoughts and observations from initial testing and I would love it if someone could prove me wrong on either of these points.</p> <p>I am using Python 2.6.6.</p> <p><strong>Edit:</strong><br> Get all Linux process IDs:</p> <pre><code>try: processDirectories = os.listdir(self.PROCESS_DIRECTORY) except IOError: return [] return [pid for pid in processDirectories if pid.isdigit()] </code></pre> <p>Get all Windows process IDs:</p> <pre><code>import ctypes, ctypes.wintypes Psapi = ctypes.WinDLL('Psapi.dll') EnumProcesses = self.Psapi.EnumProcesses EnumProcesses.restype = ctypes.wintypes.BOOL count = 50 while True: # Build arguments to EnumProcesses processIds = (ctypes.wintypes.DWORD*count)() size = ctypes.sizeof(processIds) bytes_returned = ctypes.wintypes.DWORD() # Call enum processes to find all processes if self.EnumProcesses(ctypes.byref(processIds), size, ctypes.byref(bytes_returned)): if bytes_returned.value &amp;lt size: return processIds else: # We weren't able to get all the processes so double our size and try again count *= 2 else: print "EnumProcesses failed" sys.exit() </code></pre> <p><a href="https://stackoverflow.com/questions/6980246/how-can-i-find-a-process-by-name-and-kill-using-ctypes">Windows code is from here</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload