As unit test code using python-based multiprocessing

I have code that uses multiprocessing.Pool to fork workers and execute a task in parallel. I am trying to find the right way to run unit tests of this code.

Note. I am not trying to test serial code test cases in parallel, which I know as nose-enabled packages.

If I write a test function that checks parallel code and try to run tests with the nose with:, the nosetests tests/test_function.pynon-parallel test runs correctly, but the parallel tests fail when the multiprocessor tries to use fork because main is not imported

      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__

It just repeats until I finish the task. I can run the tests successfully if I change tests/test_function.pyto include:

if __name__ == '__main__':
    import nose
    nose.main()

and then do with python tests\test_function.py

So, what is the “right” way to do this, which will integrate with the unit test package (shouldn't be the nose)?

Environ: Python 2.7.10 amd64 for Windows 7 64-bit

+6
source share
3 answers

It looks like (see http://7fttallrussian.blogspot.com/2014/04/fix-for-bug-with-unittest-and.html )

Pythons : 2.7.6 (.. 2.x 17 2014 ), unittest Windows.... Pythons 3.x, 2.x

, :

  • (- . )
  • :

( ) " ":

import unittest
import sys
class TestSample(unittest.TestCase):
    def test(self):

        # To fix the Windows forking system it necessary to point __main__ to
        # the module we want to execute in the forked process
        old_main =                          sys.modules["__main__"]
        old_main_file =                     sys.modules["__main__"].__file__
        sys.modules["__main__"] =           sys.modules["app"]
        sys.modules["__main__"].__file__ =  sys.modules["app"].__file__

        # do your testing here

        sys.modules["__main__"] =           old_main
        sys.modules["__main__"].__file__ =  old_main_file

: ( ). , , . - , .

+3

, python mock. . (Process Pool). , .

+1

. (Selenium + Bottle, python 3.6, Ubuntu 18), ( ) Selenium Chrome ( ) . , ( , ) . , , . , ?

0

Source: https://habr.com/ru/post/1611630/


All Articles