Python 2.7 tests are failing due to import error when trying to import unittest.mock
make test output
```
(. venv/bin/activate; \
tox; \
)
GLOB sdist-make: /home/cgmcintyre/devel/metaparticle/package/python/setup.py
py2 inst-nodeps: /home/cgmcintyre/devel/metaparticle/package/python/.tox/dist/metaparticle_pkg-0.6.4.zip
py2 installed: attrs==17.4.0,backports.ssl-match-hostname==3.5.0.1,certifi==2018.1.18,chardet==3.0.4,configparser==3.5.0,coverage==4.5.1,docker==2.7.0,docker-pycreds==0.2.2,enum34==1.1.6,flake8==3.5.0,funcsigs==1.0.2,idna==2.6,ipaddress==1.0.19,mccabe==0.6.1,metaparticle-pkg==0.6.4,mock==2.0.0,pbr==3.1.1,pluggy==0.6.0,py==1.5.2,pycodestyle==2.3.1,pyflakes==1.6.0,pytest==3.4.1,pytest-cov==2.5.1,pytest-mock==1.7.0,requests==2.18.4,six==1.11.0,urllib3==1.22,websocket-client==0.47.0
py2 runtests: PYTHONHASHSEED='1510474863'
py2 runtests: commands[0] | coverage erase
py2 runtests: commands[1] | coverage run --source metaparticle_pkg -m py.test
============================= test session starts ==============================
platform linux2 -- Python 2.7.14+, pytest-3.4.1, py-1.5.2, pluggy-0.6.0
rootdir: /home/cgmcintyre/devel/metaparticle/package/python, inifile:
plugins: mock-1.7.0, cov-2.5.1
collected 0 items / 5 errors
==================================== ERRORS ====================================
____ ERROR collecting metaparticle_pkg/builder/test/test_docker_builder.py _____
ImportError while importing test module '/home/cgmcintyre/devel/metaparticle/package/python/metaparticle_pkg/builder/test/test_docker_builder.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
metaparticle_pkg/builder/test/test_docker_builder.py:6: in
from unittest.mock import patch
E ImportError: No module named mock
_____ ERROR collecting metaparticle_pkg/runner/test/test_docker_runner.py ______
ImportError while importing test module '/home/cgmcintyre/devel/metaparticle/package/python/metaparticle_pkg/runner/test/test_docker_runner.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
metaparticle_pkg/runner/test/test_docker_runner.py:6: in
from unittest.mock import patch, MagicMock
E ImportError: No module named mock
______ ERROR collecting metaparticle_pkg/runner/test/test_metaparticle.py ______
ImportError while importing test module '/home/cgmcintyre/devel/metaparticle/package/python/metaparticle_pkg/runner/test/test_metaparticle.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
metaparticle_pkg/runner/test/test_metaparticle.py:5: in
from unittest.mock import patch, MagicMock, mock_open
E ImportError: No module named mock
_________ ERROR collecting metaparticle_pkg/test/test_containerize.py __________
ImportError while importing test module '/home/cgmcintyre/devel/metaparticle/package/python/metaparticle_pkg/test/test_containerize.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
metaparticle_pkg/test/test_containerize.py:5: in
from unittest.mock import patch, mock_open, MagicMock
E ImportError: No module named mock
____________ ERROR collecting metaparticle_pkg/test/test_option.py _____________
ImportError while importing test module '/home/cgmcintyre/devel/metaparticle/package/python/metaparticle_pkg/test/test_option.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
metaparticle_pkg/test/test_option.py:6: in
from unittest.mock import patch, MagicMock
E ImportError: No module named mock
!!!!!!!!!!!!!!!!!!! Interrupted: 5 errors during collection !!!!!!!!!!!!!!!!!!!!
=========================== 5 error in 0.31 seconds ============================
ERROR: InvocationError: '/home/cgmcintyre/devel/metaparticle/package/python/.tox/py2/bin/coverage run --source metaparticle_pkg -m py.test'
py3 inst-nodeps: /home/cgmcintyre/devel/metaparticle/package/python/.tox/dist/metaparticle_pkg-0.6.4.zip
py3 installed: attrs==17.4.0,certifi==2018.1.18,chardet==3.0.4,coverage==4.5.1,docker==2.7.0,docker-pycreds==0.2.2,flake8==3.5.0,idna==2.6,mccabe==0.6.1,metaparticle-pkg==0.6.4,mock==2.0.0,pbr==3.1.1,pluggy==0.6.0,py==1.5.2,pycodestyle==2.3.1,pyflakes==1.6.0,pytest==3.4.1,pytest-cov==2.5.1,pytest-mock==1.7.0,requests==2.18.4,six==1.11.0,urllib3==1.22,websocket-client==0.47.0
py3 runtests: PYTHONHASHSEED='1510474863'
py3 runtests: commands[0] | coverage erase
py3 runtests: commands[1] | coverage run --source metaparticle_pkg -m py.test
============================= test session starts ==============================
platform linux -- Python 3.6.4, pytest-3.4.1, py-1.5.2, pluggy-0.6.0
rootdir: /home/cgmcintyre/devel/metaparticle/package/python, inifile:
plugins: mock-1.7.0, cov-2.5.1
collected 26 items
metaparticle_pkg/builder/test/test_docker_builder.py .. [ 7%]
metaparticle_pkg/runner/test/test_docker_runner.py ... [ 19%]
metaparticle_pkg/runner/test/test_metaparticle.py ..... [ 38%]
metaparticle_pkg/test/test_containerize.py ............ [ 84%]
metaparticle_pkg/test/test_option.py .... [100%]
========================== 26 passed in 0.38 seconds ===========================
py3 runtests: commands[2] | coverage report
Name Stmts Miss Cover
--------------------------------------------------------------------------
metaparticle_pkg/__init__.py 2 0 100%
metaparticle_pkg/builder/__init__.py 5 3 40%
metaparticle_pkg/builder/docker_builder.py 32 19 41%
metaparticle_pkg/builder/test/__init__.py 0 0 100%
metaparticle_pkg/builder/test/test_docker_builder.py 16 1 94%
metaparticle_pkg/containerize.py 56 3 95%
metaparticle_pkg/option.py 33 8 76%
metaparticle_pkg/runner/__init__.py 8 5 38%
metaparticle_pkg/runner/docker_runner.py 24 1 96%
metaparticle_pkg/runner/metaparticle.py 25 0 100%
metaparticle_pkg/runner/test/__init__.py 0 0 100%
metaparticle_pkg/runner/test/test_docker_runner.py 31 1 97%
metaparticle_pkg/runner/test/test_metaparticle.py 58 1 98%
metaparticle_pkg/test/__init__.py 0 0 100%
metaparticle_pkg/test/test_containerize.py 97 1 99%
metaparticle_pkg/test/test_option.py 44 3 93%
--------------------------------------------------------------------------
TOTAL 431 46 89%
___________________________________ summary ____________________________________
ERROR: py2: commands failed
py3: commands succeeded
Makefile:4: recipe for target 'test' failed
make: *** [test] Error 1
```
Cause
All python unit tests import functions from unittest.mock, however unittest.mock is only included in the standard library from python version 3.3 onwards (see https://pypi.python.org/pypi/mock).
Bug
Python 2.7 tests are failing due to import error when trying to import unittest.mock
make test output
``` (. venv/bin/activate; \ tox; \ ) GLOB sdist-make: /home/cgmcintyre/devel/metaparticle/package/python/setup.py py2 inst-nodeps: /home/cgmcintyre/devel/metaparticle/package/python/.tox/dist/metaparticle_pkg-0.6.4.zip py2 installed: attrs==17.4.0,backports.ssl-match-hostname==3.5.0.1,certifi==2018.1.18,chardet==3.0.4,configparser==3.5.0,coverage==4.5.1,docker==2.7.0,docker-pycreds==0.2.2,enum34==1.1.6,flake8==3.5.0,funcsigs==1.0.2,idna==2.6,ipaddress==1.0.19,mccabe==0.6.1,metaparticle-pkg==0.6.4,mock==2.0.0,pbr==3.1.1,pluggy==0.6.0,py==1.5.2,pycodestyle==2.3.1,pyflakes==1.6.0,pytest==3.4.1,pytest-cov==2.5.1,pytest-mock==1.7.0,requests==2.18.4,six==1.11.0,urllib3==1.22,websocket-client==0.47.0 py2 runtests: PYTHONHASHSEED='1510474863' py2 runtests: commands[0] | coverage erase py2 runtests: commands[1] | coverage run --source metaparticle_pkg -m py.test ============================= test session starts ============================== platform linux2 -- Python 2.7.14+, pytest-3.4.1, py-1.5.2, pluggy-0.6.0 rootdir: /home/cgmcintyre/devel/metaparticle/package/python, inifile: plugins: mock-1.7.0, cov-2.5.1 collected 0 items / 5 errors ==================================== ERRORS ==================================== ____ ERROR collecting metaparticle_pkg/builder/test/test_docker_builder.py _____ ImportError while importing test module '/home/cgmcintyre/devel/metaparticle/package/python/metaparticle_pkg/builder/test/test_docker_builder.py'. Hint: make sure your test modules/packages have valid Python names. Traceback: metaparticle_pkg/builder/test/test_docker_builder.py:6: in
from unittest.mock import patch
E ImportError: No module named mock
_____ ERROR collecting metaparticle_pkg/runner/test/test_docker_runner.py ______
ImportError while importing test module '/home/cgmcintyre/devel/metaparticle/package/python/metaparticle_pkg/runner/test/test_docker_runner.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
metaparticle_pkg/runner/test/test_docker_runner.py:6: in
from unittest.mock import patch, MagicMock
E ImportError: No module named mock
______ ERROR collecting metaparticle_pkg/runner/test/test_metaparticle.py ______
ImportError while importing test module '/home/cgmcintyre/devel/metaparticle/package/python/metaparticle_pkg/runner/test/test_metaparticle.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
metaparticle_pkg/runner/test/test_metaparticle.py:5: in
from unittest.mock import patch, MagicMock, mock_open
E ImportError: No module named mock
_________ ERROR collecting metaparticle_pkg/test/test_containerize.py __________
ImportError while importing test module '/home/cgmcintyre/devel/metaparticle/package/python/metaparticle_pkg/test/test_containerize.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
metaparticle_pkg/test/test_containerize.py:5: in
from unittest.mock import patch, mock_open, MagicMock
E ImportError: No module named mock
____________ ERROR collecting metaparticle_pkg/test/test_option.py _____________
ImportError while importing test module '/home/cgmcintyre/devel/metaparticle/package/python/metaparticle_pkg/test/test_option.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
metaparticle_pkg/test/test_option.py:6: in
from unittest.mock import patch, MagicMock
E ImportError: No module named mock
!!!!!!!!!!!!!!!!!!! Interrupted: 5 errors during collection !!!!!!!!!!!!!!!!!!!!
=========================== 5 error in 0.31 seconds ============================
ERROR: InvocationError: '/home/cgmcintyre/devel/metaparticle/package/python/.tox/py2/bin/coverage run --source metaparticle_pkg -m py.test'
py3 inst-nodeps: /home/cgmcintyre/devel/metaparticle/package/python/.tox/dist/metaparticle_pkg-0.6.4.zip
py3 installed: attrs==17.4.0,certifi==2018.1.18,chardet==3.0.4,coverage==4.5.1,docker==2.7.0,docker-pycreds==0.2.2,flake8==3.5.0,idna==2.6,mccabe==0.6.1,metaparticle-pkg==0.6.4,mock==2.0.0,pbr==3.1.1,pluggy==0.6.0,py==1.5.2,pycodestyle==2.3.1,pyflakes==1.6.0,pytest==3.4.1,pytest-cov==2.5.1,pytest-mock==1.7.0,requests==2.18.4,six==1.11.0,urllib3==1.22,websocket-client==0.47.0
py3 runtests: PYTHONHASHSEED='1510474863'
py3 runtests: commands[0] | coverage erase
py3 runtests: commands[1] | coverage run --source metaparticle_pkg -m py.test
============================= test session starts ==============================
platform linux -- Python 3.6.4, pytest-3.4.1, py-1.5.2, pluggy-0.6.0
rootdir: /home/cgmcintyre/devel/metaparticle/package/python, inifile:
plugins: mock-1.7.0, cov-2.5.1
collected 26 items
metaparticle_pkg/builder/test/test_docker_builder.py .. [ 7%]
metaparticle_pkg/runner/test/test_docker_runner.py ... [ 19%]
metaparticle_pkg/runner/test/test_metaparticle.py ..... [ 38%]
metaparticle_pkg/test/test_containerize.py ............ [ 84%]
metaparticle_pkg/test/test_option.py .... [100%]
========================== 26 passed in 0.38 seconds ===========================
py3 runtests: commands[2] | coverage report
Name Stmts Miss Cover
--------------------------------------------------------------------------
metaparticle_pkg/__init__.py 2 0 100%
metaparticle_pkg/builder/__init__.py 5 3 40%
metaparticle_pkg/builder/docker_builder.py 32 19 41%
metaparticle_pkg/builder/test/__init__.py 0 0 100%
metaparticle_pkg/builder/test/test_docker_builder.py 16 1 94%
metaparticle_pkg/containerize.py 56 3 95%
metaparticle_pkg/option.py 33 8 76%
metaparticle_pkg/runner/__init__.py 8 5 38%
metaparticle_pkg/runner/docker_runner.py 24 1 96%
metaparticle_pkg/runner/metaparticle.py 25 0 100%
metaparticle_pkg/runner/test/__init__.py 0 0 100%
metaparticle_pkg/runner/test/test_docker_runner.py 31 1 97%
metaparticle_pkg/runner/test/test_metaparticle.py 58 1 98%
metaparticle_pkg/test/__init__.py 0 0 100%
metaparticle_pkg/test/test_containerize.py 97 1 99%
metaparticle_pkg/test/test_option.py 44 3 93%
--------------------------------------------------------------------------
TOTAL 431 46 89%
___________________________________ summary ____________________________________
ERROR: py2: commands failed
py3: commands succeeded
Makefile:4: recipe for target 'test' failed
make: *** [test] Error 1
```
Cause
All python unit tests import functions from
unittest.mock
, howeverunittest.mock
is only included in the standard library from python version 3.3 onwards (see https://pypi.python.org/pypi/mock).Fix
Use https://www.python.org/dev/peps/pep-0508/#environment-markers in
requirements.txt
andsetup.py
to installmock
if python version < 3.3Change imports to use
unittest.mock
if python version >= 3.3, otherwise usemock
package.