aboutsummaryrefslogtreecommitdiff
path: root/absl/testing
diff options
context:
space:
mode:
authorYilei Yang <yileiyang@google.com>2022-01-24 12:08:19 -0800
committerCopybara-Service <copybara-worker@google.com>2022-01-24 12:08:51 -0800
commitc7c6f8b2918bb280b290a640ebb608753ca79a6f (patch)
tree0b0db093d730fc2400d6f04b348154f4bfc3467b /absl/testing
parentd12ab3ba5dba65e1c828bdb7427d15ae28641c13 (diff)
downloadabsl-py-c7c6f8b2918bb280b290a640ebb608753ca79a6f.tar.gz
Remove PY2-ism in absl's own unit tests.
PiperOrigin-RevId: 423878454 Change-Id: Iadc82651ecd39954cfd2af6b4bae2a98388fd75b
Diffstat (limited to 'absl/testing')
-rw-r--r--absl/testing/BUILD4
-rw-r--r--absl/testing/tests/absltest_test.py102
-rw-r--r--absl/testing/tests/parameterized_test.py8
-rw-r--r--absl/testing/tests/xml_reporter_test.py100
4 files changed, 58 insertions, 156 deletions
diff --git a/absl/testing/BUILD b/absl/testing/BUILD
index d85b84c..de77bcc 100644
--- a/absl/testing/BUILD
+++ b/absl/testing/BUILD
@@ -193,7 +193,6 @@ py_test(
":absltest",
":parameterized",
":tests/absltest_env",
- "@six_archive//:six",
],
)
@@ -230,7 +229,6 @@ py_test(
":absltest",
":parameterized",
"//absl:_collections_abc",
- "@six_archive//:six",
],
)
@@ -246,8 +244,6 @@ py_test(
":parameterized",
":xml_reporter",
"//absl/logging",
- "@mock_archive//:mock",
- "@six_archive//:six",
],
)
diff --git a/absl/testing/tests/absltest_test.py b/absl/testing/tests/absltest_test.py
index 6e8503c..48eeca8 100644
--- a/absl/testing/tests/absltest_test.py
+++ b/absl/testing/tests/absltest_test.py
@@ -14,19 +14,15 @@
"""Tests for absltest."""
-from __future__ import absolute_import
-from __future__ import division
-from __future__ import print_function
-
import collections
import contextlib
import io
import os
+import pathlib
import re
import stat
import string
import subprocess
-import sys
import tempfile
import unittest
@@ -34,14 +30,6 @@ from absl.testing import _bazelize_command
from absl.testing import absltest
from absl.testing import parameterized
from absl.testing.tests import absltest_env
-import six
-
-try:
- import pathlib
-except ImportError: # PY2
- pathlib = None
-
-PY_VERSION_2 = sys.version_info[0] == 2
class HelperMixin(object):
@@ -52,7 +40,7 @@ class HelperMixin(object):
def run_helper(self, test_id, args, env_overrides, expect_success):
env = absltest_env.inherited_env()
- for key, value in six.iteritems(env_overrides):
+ for key, value in env_overrides.items():
if value is None:
if key in env:
del env[key]
@@ -266,11 +254,6 @@ class TestCaseTest(absltest.TestCase, HelperMixin):
# Test that sequences of unhashable objects can be tested for sameness:
self.assertSameElements([[1, 2], [3, 4]], [[3, 4], [1, 2]])
- if PY_VERSION_2:
- # dict's are no longer valid for < comparison in Python 3 making them
- # unsortable (yay, sanity!). But we need to preserve this old behavior
- # when running under Python 2.
- self.assertSameElements([{'a': 1}, {'b': 2}], [{'b': 2}, {'a': 1}])
self.assertRaises(AssertionError, self.assertSameElements, [[1]], [[2]])
def test_assert_items_equal_hotfix(self):
@@ -838,14 +821,10 @@ test case
? +++++++++++++++++++++
+ own implementation that does not subclass from TestCase, of course.
"""
- types = (str, unicode) if PY_VERSION_2 else (str,)
-
- for type1 in types:
- for type2 in types:
- self.assertRaisesWithLiteralMatch(AssertionError, sample_text_error,
- self.assertMultiLineEqual,
- type1(sample_text),
- type2(revised_sample_text))
+ self.assertRaisesWithLiteralMatch(AssertionError, sample_text_error,
+ self.assertMultiLineEqual,
+ sample_text,
+ revised_sample_text)
self.assertRaises(AssertionError, self.assertMultiLineEqual, (1, 2), 'str')
self.assertRaises(AssertionError, self.assertMultiLineEqual, 'str', (1, 2))
@@ -1091,10 +1070,6 @@ test case
self.assertTotallyOrdered([1], [2])
self.assertTotallyOrdered([1, 1, 1])
self.assertTotallyOrdered([(1, 1)], [(1, 2)], [(2, 1)])
- if PY_VERSION_2:
- # In Python 3 comparing different types of elements is not supported.
- self.assertTotallyOrdered([None], [1], [2])
- self.assertTotallyOrdered([1, 1, 1], ['a string'])
# From the docstring.
class A(object):
@@ -1149,30 +1124,15 @@ test case
"""Like A, but not hashable."""
__hash__ = None
- if PY_VERSION_2:
- self.assertTotallyOrdered(
- [None], # None should come before everything else.
- [1], # Integers sort earlier.
- [A(1, 'a')],
- [A(2, 'b')], # 2 is after 1.
- [
- A(3, 'c'),
- B(3, 'd'),
- B(3, 'e') # The second argument is irrelevant.
- ],
- [A(4, 'z')],
- ['foo']) # Strings sort last.
- else:
- # Python 3 does not define ordering across different types.
- self.assertTotallyOrdered(
- [A(1, 'a')],
- [A(2, 'b')], # 2 is after 1.
- [
- A(3, 'c'),
- B(3, 'd'),
- B(3, 'e') # The second argument is irrelevant.
- ],
- [A(4, 'z')])
+ self.assertTotallyOrdered(
+ [A(1, 'a')],
+ [A(2, 'b')], # 2 is after 1.
+ [
+ A(3, 'c'),
+ B(3, 'd'),
+ B(3, 'e') # The second argument is irrelevant.
+ ],
+ [A(4, 'z')])
# Invalid.
msg = 'This is a useful message'
@@ -1224,10 +1184,7 @@ test case
def test_assert_url_equal_different(self):
msg = 'This is a useful message'
- if PY_VERSION_2:
- whole_msg = "'a' != 'b' : This is a useful message"
- else:
- whole_msg = 'This is a useful message:\n- a\n+ b\n'
+ whole_msg = 'This is a useful message:\n- a\n+ b\n'
self.assertRaisesWithLiteralMatch(AssertionError, whole_msg,
self.assertUrlEqual,
'http://a', 'http://b', msg=msg)
@@ -1267,9 +1224,6 @@ test case
{'one': 1})
self.assertSameStructure(collections.OrderedDict({'one': 1}),
collections.defaultdict(None, {'one': 1}))
- # int and long should always be treated as the same type.
- if PY_VERSION_2:
- self.assertSameStructure({long(3): 3}, {3: long(3)})
def test_same_structure_different(self):
# Different type
@@ -1483,8 +1437,7 @@ class GetCommandStderrTestCase(absltest.TestCase):
absltest.get_command_stderr(
['cat', os.path.join(tmpdir, 'file.txt')],
env=_env_for_command_tests())[1])
- if not PY_VERSION_2:
- stderr = stderr.decode('utf-8')
+ stderr = stderr.decode('utf-8')
self.assertRegex(stderr, 'No such file or directory')
@@ -1497,7 +1450,6 @@ def cm_for_test(obj):
obj.cm_state = 'exited'
-@absltest.skipIf(six.PY2, 'Python 2 does not have ExitStack')
class EnterContextTest(absltest.TestCase):
def setUp(self):
@@ -1672,25 +1624,18 @@ class EqualityAssertionTest(absltest.TestCase):
self.assertEqual(same_a, same_b)
self.assertEquals(same_a, same_b)
self.failUnlessEqual(same_a, same_b)
- if PY_VERSION_2:
- # Python 3 removes the global cmp function
- self.assertEqual(0, cmp(same_a, same_b))
self.assertFalse(same_a == different)
self.assertTrue(same_a != different)
self.assertNotEqual(same_a, different)
self.assertNotEquals(same_a, different)
self.failIfEqual(same_a, different)
- if PY_VERSION_2:
- self.assertNotEqual(0, cmp(same_a, different))
self.assertFalse(same_b == different)
self.assertTrue(same_b != different)
self.assertNotEqual(same_b, different)
self.assertNotEquals(same_b, different)
self.failIfEqual(same_b, different)
- if PY_VERSION_2:
- self.assertNotEqual(0, cmp(same_b, different))
def test_comparison_with_eq(self):
same_a = self.EqualityTestsWithEq(42)
@@ -1705,15 +1650,9 @@ class EqualityAssertionTest(absltest.TestCase):
self._perform_apple_apple_orange_checks(same_a, same_b, different)
def test_comparison_with_cmp_or_lt_eq(self):
- if PY_VERSION_2:
- # In Python 3; the __cmp__ method is no longer special.
- cmp_or_lteq_class = self.EqualityTestsWithCmp
- else:
- cmp_or_lteq_class = self.EqualityTestsWithLtEq
-
- same_a = cmp_or_lteq_class(42)
- same_b = cmp_or_lteq_class(42)
- different = cmp_or_lteq_class(1769)
+ same_a = self.EqualityTestsWithLtEq(42)
+ same_b = self.EqualityTestsWithLtEq(42)
+ different = self.EqualityTestsWithLtEq(1769)
self._perform_apple_apple_orange_checks(same_a, same_b, different)
@@ -2102,7 +2041,6 @@ class TempFileTest(absltest.TestCase, HelperMixin):
os.chmod(os.path.dirname(path), stat.S_IEXEC)
# The test should pass, even though that file cannot be deleted in teardown.
- @absltest.skipUnless(getattr(os, 'PathLike', None), 'Testing os.PathLike')
def test_temp_file_path_like(self):
tempdir = self.create_tempdir('foo')
self.assertIsInstance(tempdir, os.PathLike)
diff --git a/absl/testing/tests/parameterized_test.py b/absl/testing/tests/parameterized_test.py
index ebfa72b..7f63d01 100644
--- a/absl/testing/tests/parameterized_test.py
+++ b/absl/testing/tests/parameterized_test.py
@@ -14,18 +14,12 @@
"""Tests for absl.testing.parameterized."""
-from __future__ import absolute_import
-from __future__ import division
-from __future__ import print_function
-
import sys
import unittest
from absl._collections_abc import abc
from absl.testing import absltest
from absl.testing import parameterized
-import six
-from six.moves import range # pylint: disable=redefined-builtin
class MyOwnClass(object):
@@ -1044,7 +1038,7 @@ class CoopMetaclassCreationTest(absltest.TestCase):
def __init__(cls, name, bases, dct):
type.__init__(cls, name, bases, dct)
- for member_name, obj in six.iteritems(dct):
+ for member_name, obj in dct.items():
if member_name.startswith('test'):
setattr(cls, member_name,
lambda self, f=obj: _decorate_with_side_effects(f, self))
diff --git a/absl/testing/tests/xml_reporter_test.py b/absl/testing/tests/xml_reporter_test.py
index 1b66924..0261f64 100644
--- a/absl/testing/tests/xml_reporter_test.py
+++ b/absl/testing/tests/xml_reporter_test.py
@@ -12,11 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-from __future__ import absolute_import
-from __future__ import division
-from __future__ import print_function
-
import datetime
+import io
import os
import re
import subprocess
@@ -25,6 +22,7 @@ import tempfile
import threading
import time
import unittest
+from unittest import mock
from xml.etree import ElementTree
from xml.parsers import expat
@@ -33,11 +31,9 @@ from absl.testing import _bazelize_command
from absl.testing import absltest
from absl.testing import parameterized
from absl.testing import xml_reporter
-import mock
-import six
-class StringIOWriteLn(six.StringIO):
+class StringIOWriteLn(io.StringIO):
def writeln(self, line):
self.write(line + '\n')
@@ -67,14 +63,16 @@ def xml_escaped_exception_type(exception_type):
OUTPUT_STRING = '\n'.join([
r'<\?xml version="1.0"\?>',
- '<testsuites name="" tests="%(tests)d" failures="%(failures)d"'
- ' errors="%(errors)d" time="%(run_time).1f" timestamp="%(start_time)s">',
- '<testsuite name="%(suite_name)s" tests="%(tests)d"'
- ' failures="%(failures)d" errors="%(errors)d" time="%(run_time).1f" timestamp="%(start_time)s">',
- ' <testcase name="%(test_name)s" status="%(status)s" result="%(result)s"'
- ' time="%(run_time).1f" classname="%(classname)s"'
- ' timestamp="%(start_time)s">%(message)s', ' </testcase>', '</testsuite>',
- '</testsuites>'
+ ('<testsuites name="" tests="%(tests)d" failures="%(failures)d"'
+ ' errors="%(errors)d" time="%(run_time).1f" timestamp="%(start_time)s">'),
+ ('<testsuite name="%(suite_name)s" tests="%(tests)d"'
+ ' failures="%(failures)d" errors="%(errors)d" time="%(run_time).1f"'
+ ' timestamp="%(start_time)s">'),
+ (' <testcase name="%(test_name)s" status="%(status)s" result="%(result)s"'
+ ' time="%(run_time).1f" classname="%(classname)s"'
+ ' timestamp="%(start_time)s">%(message)s'),
+ ' </testcase>', '</testsuite>',
+ '</testsuites>',
])
FAILURE_MESSAGE = r"""
@@ -97,8 +95,8 @@ UNICODE_MESSAGE = r"""
raise AssertionError\(u'\\xe9'\)
AssertionError: {0}
\]\]></%s>""".format(
- r'\\xe9' if six.PY2 else r'\xe9',
- xml_escaped_exception_type(AssertionError))
+ r'\xe9',
+ xml_escaped_exception_type(AssertionError))
NEWLINE_MESSAGE = r"""
<%s message="{0}" type="{1}"><!\[CDATA\[Traceback \(most recent call last\):
@@ -113,9 +111,10 @@ AssertionError: {3}
UNEXPECTED_SUCCESS_MESSAGE = '\n'.join([
'',
- r' <error message="" type=""><!\[CDATA\[Test case '
- r'__main__.MockTest.unexpectedly_passing_test should have failed, '
- r'but passed.\]\]></error>'])
+ (r' <error message="" type=""><!\[CDATA\[Test case '
+ r'__main__.MockTest.unexpectedly_passing_test should have failed, '
+ r'but passed.\]\]></error>'),
+])
UNICODE_ERROR_MESSAGE = UNICODE_MESSAGE % ('error', 'error')
NEWLINE_ERROR_MESSAGE = NEWLINE_MESSAGE % ('error', 'error')
@@ -124,8 +123,9 @@ NEWLINE_ERROR_MESSAGE = NEWLINE_MESSAGE % ('error', 'error')
class TextAndXMLTestResultTest(absltest.TestCase):
def setUp(self):
+ super().setUp()
self.stream = StringIOWriteLn()
- self.xml_stream = six.StringIO()
+ self.xml_stream = io.StringIO()
def _make_result(self, times):
timer = mock.Mock()
@@ -1060,50 +1060,24 @@ class XmlReporterFixtureTest(absltest.TestCase):
'error': 'test Errored!'}]}])
def test_set_up_failure(self):
- if six.PY2:
- # A failure in setUp() produces an error (not a failure), which is
- # inconsistent with the Python unittest documentation. In Python
- # 2.7, the bug appears to be in unittest.TestCase.run() method.
- # Although it correctly checks for a SkipTest exception, it does
- # not check for a failureException.
- self._run_test(
- flag='--set_up_fail',
- num_errors=1,
- num_failures=0,
- suites=[{'name': 'FailableTest',
- 'cases': [{'name': 'test',
- 'classname': '__main__.FailableTest',
- 'error': 'setUp Failed!'}]}])
- else:
- self._run_test(
- flag='--set_up_fail',
- num_errors=0,
- num_failures=1,
- suites=[{'name': 'FailableTest',
- 'cases': [{'name': 'test',
- 'classname': '__main__.FailableTest',
- 'failure': 'setUp Failed!'}]}])
+ self._run_test(
+ flag='--set_up_fail',
+ num_errors=0,
+ num_failures=1,
+ suites=[{'name': 'FailableTest',
+ 'cases': [{'name': 'test',
+ 'classname': '__main__.FailableTest',
+ 'failure': 'setUp Failed!'}]}])
def test_tear_down_failure(self):
- if six.PY2:
- # See comment in test_set_up_failure().
- self._run_test(
- flag='--tear_down_fail',
- num_errors=1,
- num_failures=0,
- suites=[{'name': 'FailableTest',
- 'cases': [{'name': 'test',
- 'classname': '__main__.FailableTest',
- 'error': 'tearDown Failed!'}]}])
- else:
- self._run_test(
- flag='--tear_down_fail',
- num_errors=0,
- num_failures=1,
- suites=[{'name': 'FailableTest',
- 'cases': [{'name': 'test',
- 'classname': '__main__.FailableTest',
- 'failure': 'tearDown Failed!'}]}])
+ self._run_test(
+ flag='--tear_down_fail',
+ num_errors=0,
+ num_failures=1,
+ suites=[{'name': 'FailableTest',
+ 'cases': [{'name': 'test',
+ 'classname': '__main__.FailableTest',
+ 'failure': 'tearDown Failed!'}]}])
def test_test_fail(self):
self._run_test(