title: How to unit-test code that uses python-multiprocessing
body: <p>I have some code that uses multiprocessing.Pool to fork workers and perform a task in parallel. I'm trying to find the <em>right way</em> to run unit tests of this code.</p>
  
  <p>Note I am <em>not</em> trying to test serial code test cases in parallel which I know packages like nose support.</p>
  
  <p>If I write a test function that tests said parallel code, and attempt to run the tests with nose via: <code>nosetests tests/test_function.py</code> the non-parallel test execute properly but the parallel tests fail when multiprocessing tries to fork because main is not importable:</p>
  
  <pre><code>      File "C:\python-2.7.10.amd64\lib\multiprocessing\forking.py", line 488, in prepare
  assert main_name not in sys.modules, main_name
  AssertionError: __main__
      assert main_name not in sys.modules, main_name
  AssertionError:     _assert main_name not in sys.modules, main_name
  _main__AssertionError
  : __main__
  </code></pre>
  
  <p>Which just repeats until I terminate the task. I can run the tests successfully if I modify <code>tests/test_function.py</code> to include:</p>
  
  <pre><code>if __name__ == '__main__':
      import nose
      nose.main()
  </code></pre>
  
  <p>and then execute with <code>python tests\test_function.py</code></p>
  
  <p>So what is the "right" way to do this that will integrate with a unit test package (doesn't have to be nose)?</p>
  
  <p>Environ: Python 2.7.10 amd64 on Windows 7 64-bit</p>
  
  <p>Update 2020: With python 3 and pytest, this is not an issue, suggest upgrades!</p>