From 0f883090916cf84309dddb1cdf6dd51265e36a0a Mon Sep 17 00:00:00 2001 From: Javier Pena Date: Mon, 13 Apr 2020 18:25:23 +0200 Subject: [PATCH] Make mock import python 2.7 compatible In python3.x mock is now part of unittest library. For being still compatible with python 2.7 we need to catch import error when trying to import unittest.mock. Also add an __init__.py file in validations_common/library, to allow all unit tests to import successfully. This is similar to [1] for validations-libs. [1]- https://review.opendev.org/716597 Change-Id: I6a81dc2640e9c4af3a07158bea95ebaec9fd466b --- validations_common/library/__init__.py | 0 .../tests/library/test_check_package_update.py | 8 ++++++-- validations_common/tests/library/test_reportentry.py | 5 ++++- 3 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 validations_common/library/__init__.py diff --git a/validations_common/library/__init__.py b/validations_common/library/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/validations_common/tests/library/test_check_package_update.py b/validations_common/tests/library/test_check_package_update.py index 173d0fb..b5f8f69 100644 --- a/validations_common/tests/library/test_check_package_update.py +++ b/validations_common/tests/library/test_check_package_update.py @@ -12,8 +12,12 @@ # License for the specific language governing permissions and limitations # under the License. -from unittest.mock import MagicMock -from unittest.mock import patch +try: + from unittest.mock import MagicMock + from unittest.mock import patch +except ImportError: + from mock import MagicMock + from mock import patch from validations_common.library.check_package_update import check_update from validations_common.library.check_package_update import get_package_details diff --git a/validations_common/tests/library/test_reportentry.py b/validations_common/tests/library/test_reportentry.py index 92075a5..d97ca34 100644 --- a/validations_common/tests/library/test_reportentry.py +++ b/validations_common/tests/library/test_reportentry.py @@ -23,7 +23,10 @@ Tests for `reportentry` module. import validations_common.library.reportentry as validation from validations_common.tests import base -from unittest import mock +try: + from unittest import mock +except ImportError: + import mock reason = "Reason #1" recommendation = ['Recommendation #1']