From 7e0c44e21fa387f8fa681e257bf8abebfdc29177 Mon Sep 17 00:00:00 2001 From: Rodolfo Alonso Hernandez Date: Fri, 10 Jan 2020 16:28:13 +0000 Subject: [PATCH] Remove references to unittest2 library Library "unittest2" has not released a new version since Jun 30 2015 [1]. Neutron should remove the references to this library and point to "unittest" instead. [1] https://pypi.org/project/unittest2/#history Change-Id: I7d55adc262280c0c2f13b9b81ecc582e1729afa0 Closes-Bug: #1859190 --- HACKING.rst | 5 +---- lower-constraints.txt | 1 - neutron/hacking/checks.py | 15 --------------- neutron/tests/common/base.py | 5 ++--- neutron/tests/functional/pecan_wsgi/__init__.py | 4 ++-- neutron/tests/tools.py | 4 ++-- neutron/tests/unit/hacking/test_checks.py | 12 ------------ neutron/tests/unit/tests/test_base.py | 4 ++-- 8 files changed, 9 insertions(+), 41 deletions(-) diff --git a/HACKING.rst b/HACKING.rst index 0036a99c6cd..19b0098170c 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -21,7 +21,6 @@ Below you can find a list of checks specific to this repository. - [N331] Detect wrong usage with assertTrue(isinstance()). - [N332] Use assertEqual(expected_http_code, observed_http_code) instead of assertEqual(observed_http_code, expected_http_code). -- [N334] Use unittest2 uniformly across Neutron. - [N340] Check usage of .i18n (and neutron.i18n) - [N341] Check usage of _ from python builtins - [N343] Production code must not import from neutron.tests.* @@ -46,9 +45,7 @@ without the patch and passes with the patch. All unittest classes must ultimately inherit from testtools.TestCase. In the Neutron test suite, this should be done by inheriting from -neutron.tests.base.BaseTestCase. If the third party unittest library has to -be used directly then it is recommended to use unittest2 as it contains bug -fixes to unittest for all versions of Python prior to version 3.5. +neutron.tests.base.BaseTestCase. All setUp and tearDown methods must upcall using the super() method. tearDown methods should be avoided and addCleanup calls should be preferred. diff --git a/lower-constraints.txt b/lower-constraints.txt index ac9daf9bb6c..85042b5d6a7 100644 --- a/lower-constraints.txt +++ b/lower-constraints.txt @@ -146,7 +146,6 @@ testtools==2.2.0 tooz==1.58.0 tinyrpc==0.6 traceback2==1.4.0 -unittest2==1.1.0 vine==1.1.4 waitress==1.1.0 WebOb==1.8.2 diff --git a/neutron/hacking/checks.py b/neutron/hacking/checks.py index f7b23d89ecd..b11c0aae9b6 100644 --- a/neutron/hacking/checks.py +++ b/neutron/hacking/checks.py @@ -15,7 +15,6 @@ import os import re -from hacking import core from neutron_lib.hacking import checks @@ -42,8 +41,6 @@ def flake8ext(f): # neutron/tests/unit/hacking/test_checks.py -unittest_imports_dot = re.compile(r"\bimport[\s]+unittest\b") -unittest_imports_from = re.compile(r"\bfrom[\s]+unittest\b") filter_match = re.compile(r".*filter\(lambda ") tests_imports_dot = re.compile(r"\bimport[\s]+neutron.tests\b") @@ -204,17 +201,6 @@ def check_builtins_gettext(logical_line, tokens, filename, lines, noqa): yield (0, msg) -@core.flake8ext -@core.off_by_default -def check_unittest_imports(logical_line): - """N334 - Use unittest2 instead of unittest""" - if (re.match(unittest_imports_from, logical_line) or - re.match(unittest_imports_dot, logical_line)): - msg = "N334: '%s' must be used instead of '%s'." % ( - logical_line.replace('unittest', 'unittest2'), logical_line) - yield (0, msg) - - @flake8ext def check_no_imports_from_tests(logical_line, filename, noqa): """N343 Production code must not import from neutron.tests.* @@ -270,7 +256,6 @@ def factory(register): register(check_assertequal_for_httpcode) register(check_oslo_i18n_wrapper) register(check_builtins_gettext) - register(check_unittest_imports) register(check_no_imports_from_tests) register(check_python3_no_filter) register(check_no_sqlalchemy_event_import) diff --git a/neutron/tests/common/base.py b/neutron/tests/common/base.py index bad6db17cec..597d71dd0ad 100644 --- a/neutron/tests/common/base.py +++ b/neutron/tests/common/base.py @@ -12,10 +12,10 @@ # import functools +import unittest from neutron_lib import constants as n_const import testtools.testcase -import unittest2.case from neutron.common import utils from neutron.tests import base @@ -61,8 +61,7 @@ def no_skip_on_missing_deps(wrapped): def wrapper(*args, **kwargs): try: return wrapped(*args, **kwargs) - except (testtools.TestCase.skipException, - unittest2.case.SkipTest) as e: + except (testtools.TestCase.skipException, unittest.SkipTest) as e: if base.bool_from_env('OS_FAIL_ON_MISSING_DEPS'): tools.fail( '%s cannot be skipped because OS_FAIL_ON_MISSING_DEPS ' diff --git a/neutron/tests/functional/pecan_wsgi/__init__.py b/neutron/tests/functional/pecan_wsgi/__init__.py index 3ddd20d8c4a..ce825253292 100644 --- a/neutron/tests/functional/pecan_wsgi/__init__.py +++ b/neutron/tests/functional/pecan_wsgi/__init__.py @@ -15,16 +15,16 @@ import os +import unittest from pecan import set_config from pecan.testing import load_test_app -import unittest2 __all__ = ['FunctionalTest'] -class FunctionalTest(unittest2.TestCase): +class FunctionalTest(unittest.TestCase): """Pecan wsgi functional test base class Used for functional tests where you need to test your diff --git a/neutron/tests/tools.py b/neutron/tests/tools.py index 9e292b392e5..069fadf9c8b 100644 --- a/neutron/tests/tools.py +++ b/neutron/tests/tools.py @@ -15,6 +15,7 @@ import datetime import random +import unittest import fixtures import netaddr @@ -24,7 +25,6 @@ from neutron_lib.utils import helpers from neutron_lib.utils import net from oslo_utils import netutils from oslo_utils import timeutils -import unittest2 # NOTE(yamahata): from neutron-lib 1.9.1, callback priority was added and @@ -159,7 +159,7 @@ def fail(msg=None): This method is equivalent to TestCase.fail without requiring a testcase instance (usefully for reducing coupling). """ - raise unittest2.TestCase.failureException(msg) + raise unittest.TestCase.failureException(msg) def get_random_string_list(i=3, n=5): diff --git a/neutron/tests/unit/hacking/test_checks.py b/neutron/tests/unit/hacking/test_checks.py index 270546eddc8..43f75c201cc 100644 --- a/neutron/tests/unit/hacking/test_checks.py +++ b/neutron/tests/unit/hacking/test_checks.py @@ -195,18 +195,6 @@ class HackingTestCase(base.BaseTestCase): 0, len(list(checks.check_assertequal_for_httpcode(pass_code, "neutron/tests/test_assert.py")))) - def test_unittest_imports(self): - f = checks.check_unittest_imports - - self.assertLinePasses(f, 'from unittest2') - self.assertLinePasses(f, 'import unittest2') - self.assertLinePasses(f, 'from unitest2 import case') - self.assertLinePasses(f, 'unittest2.TestSuite') - - self.assertLineFails(f, 'from unittest import case') - self.assertLineFails(f, 'from unittest.TestSuite') - self.assertLineFails(f, 'import unittest') - def test_check_no_imports_from_tests(self): fail_codes = ('from neutron import tests', 'from neutron.tests import base', diff --git a/neutron/tests/unit/tests/test_base.py b/neutron/tests/unit/tests/test_base.py index 0c844ef7106..595017e7d61 100644 --- a/neutron/tests/unit/tests/test_base.py +++ b/neutron/tests/unit/tests/test_base.py @@ -16,9 +16,9 @@ """Tests to test the test framework""" import sys +import unittest import eventlet.timeout -import unittest2 from neutron.tests import base @@ -62,7 +62,7 @@ class SystemExitTestCase(base.DietTestCase): def test_sysexit(self): expectedFails = [self.MyTestCase(exitcode) for exitcode in (0, 1)] - suite = unittest2.TestSuite(tests=expectedFails) + suite = unittest.TestSuite(tests=expectedFails) result = self.defaultTestResult() try: suite.run(result)