Python absl.testing.absltest.TestCase() Examples

The following are 30 code examples of absl.testing.absltest.TestCase(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module absl.testing.absltest , or try the search function .
Example #1
Source File: sim_scene_test.py    From robel with Apache License 2.0 6 votes vote down vote up
def mjpy_and_dm(fn):
    """Decorator that tests for both mujoco_py and dm_control."""

    def test_fn(self: absltest.TestCase):
        with test_model_file() as test_file_path:
            with self.subTest('mujoco_py'):
                fn(
                    self,
                    SimScene.create(
                        test_file_path, backend=SimBackend.MUJOCO_PY))
            with self.subTest('dm_control'):
                fn(
                    self,
                    SimScene.create(
                        test_file_path, backend=SimBackend.DM_CONTROL))

    return test_fn 
Example #2
Source File: xml_reporter_test.py    From abseil-py with Apache License 2.0 6 votes vote down vote up
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!'}]}]) 
Example #3
Source File: test.py    From tensorboard with Apache License 2.0 6 votes vote down vote up
def get_temp_dir(self):
        """Returns a unique temporary directory for the test to use.

        If you call this method multiple times during in a test, it will return the
        same folder. However, across different runs the directories will be
        different. This will ensure that across different runs tests will not be
        able to pollute each others environment.
        If you need multiple unique directories within a single test, you should
        use `self.create_tempdir()`, provided by `absltest.TestCase`.
          tempfile.mkdtemp(dir=self.get_temp_dir()):

        Returns:
          string, the path to the unique temporary directory created for this test.
        """
        if not self._tempdir:
            self._tempdir = self.create_tempdir().full_path
        return self._tempdir 
Example #4
Source File: test_utils_test.py    From dm_env with Apache License 2.0 6 votes vote down vote up
def _make_test_case_with_expected_failures(
    name,
    timestep_sequence,
    expected_failures):

  class NewTestCase(test_utils.EnvironmentTestMixin, absltest.TestCase):

    def make_object_under_test(self):
      return MockEnvironment(timestep_sequence)

  for method_name, exception_type in expected_failures:
    def wrapped_method(
        self, method_name=method_name, exception_type=exception_type):
      super_method = getattr(super(NewTestCase, self), method_name)
      with self.assertRaises(exception_type):
        return super_method()
    setattr(NewTestCase, method_name, wrapped_method)

  NewTestCase.__name__ = name
  return NewTestCase 
Example #5
Source File: py_typecheck_test.py    From federated with Apache License 2.0 6 votes vote down vote up
def test_check_type(self):
    try:
      self.assertEqual('foo', py_typecheck.check_type('foo', str))
      py_typecheck.check_type('foo', str)
      py_typecheck.check_type(10, int)
      py_typecheck.check_type(10, (str, int))
      py_typecheck.check_type(10, (str, int, bool, float))
    except TypeError:
      self.fail('Function {} raised TypeError unexpectedly.'.format(
          py_typecheck.check_type.__name__))
    self.assertRaisesRegex(TypeError, 'Expected .*TestCase, found int.',
                           py_typecheck.check_type, 10, absltest.TestCase)
    self.assertRaisesRegex(
        TypeError,
        'Expected foo to be of type int, found __main__.PyTypeCheckTest.',
        py_typecheck.check_type,
        self,
        int,
        label='foo')
    self.assertRaisesRegex(TypeError, 'Expected int or bool, found str.',
                           py_typecheck.check_type, 'a', (int, bool))
    self.assertRaisesRegex(TypeError,
                           'Expected int, bool, or float, found str.',
                           py_typecheck.check_type, 'a', (int, bool, float)) 
Example #6
Source File: tracing_test.py    From federated with Apache License 2.0 6 votes vote down vote up
def test_logging_blocking_method(self):

    class AClass(absltest.TestCase):

      @tracing.trace(span=True)
      def sync_method(self, foo_arg, bar_arg, arg3=None, arg4=None):
        self.assertEqual('foo', foo_arg)
        self.assertEqual('bar', bar_arg)
        self.assertIsNotNone(arg3)
        self.assertIsNotNone(arg4)
        # Sleep for 1s is used to test that we measured runtime correctly
        time.sleep(1)
        return 3

    a_class = AClass()

    result = self._test_debug_logging_with_sync_function(
        a_class.sync_method,
        r'AClass\.sync_method\. ' + ELAPSED_ONE_REGEX,
        'foo',
        'bar',
        arg3='baz',
        arg4=True)
    self.assertEqual(3, result) 
Example #7
Source File: tracing_test.py    From federated with Apache License 2.0 6 votes vote down vote up
def test_logging_non_blocking_method(self):

    class AClass(absltest.TestCase):

      @tracing.trace(span=True)
      async def async_method(self, foo_arg, bar_arg, arg3=None, arg4=None):
        self.assertEqual('foo', foo_arg)
        self.assertEqual('bar', bar_arg)
        self.assertIsNotNone(arg3)
        self.assertIsNotNone(arg4)
        await asyncio.sleep(1)
        return 3

    a_class = AClass()

    result = self._test_debug_logging_with_async_function(
        a_class.async_method,
        # Non-blocking may not run exactly one second, but we can assert it was
        # at least one second; and most importantly it should be logged.
        r'AClass\.async_method\. ' + ELAPSED_ONE_REGEX,
        'foo',
        'bar',
        arg3='baz',
        arg4=True)
    self.assertEqual(3, result) 
Example #8
Source File: unittest3_backport_test.py    From abseil-py with Apache License 2.0 6 votes vote down vote up
def test_subtest_pass(self):

    class Foo(absltest.TestCase):

      def runTest(self):
        for i in [1, 2]:
          with self.subTest(i=i):
            for j in [2, 3]:
              with self.subTest(j=j):
                pass

    result = MockTestResult()
    Foo().run(result)
    expected_success = [{'i': 1, 'j': 2}, {'i': 1, 'j': 3}, {'i': 1},
                        {'i': 2, 'j': 2}, {'i': 2, 'j': 3}, {'i': 2}]
    self.assertListEqual(result.subtest_success, expected_success) 
Example #9
Source File: unittest3_backport_test.py    From abseil-py with Apache License 2.0 6 votes vote down vote up
def test_subtest_fail_fast(self):
    # Ensure failfast works with subtest

    class Foo(absltest.TestCase):

      def runTest(self):
        with self.subTest(i=1):
          self.fail('failure')
        with self.subTest(i=2):
          self.fail('failure')
        self.fail('failure')

    result = MockTestResult()
    result.failfast = True
    Foo().run(result)
    expected_failure = [{'i': 1}]
    self.assertListEqual(expected_failure, result.subtest_failure) 
Example #10
Source File: unittest3_backport_test.py    From abseil-py with Apache License 2.0 6 votes vote down vote up
def test_subtest_legacy(self, mock_test_fail):
    # When the result object does not have addSubTest method,
    # text execution stops after the first subtest failure.

    class Foo(absltest.TestCase):

      def runTest(self):
        for i in [1, 2, 3]:
          with self.subTest(i=i):
            if i == 1:
              self.fail('failure')
            for j in [2, 3]:
              with self.subTest(j=j):
                if i * j == 6:
                  raise RuntimeError('raised by Foo.test')

    result = MockTestResultWithoutSubTest()

    Foo().run(result)
    self.assertEqual(mock_test_fail.call_count, 1) 
Example #11
Source File: unittest3_backport_test.py    From abseil-py with Apache License 2.0 6 votes vote down vote up
def test_subtest_fail(self):
    class Foo(absltest.TestCase):

      def runTest(self):
        for i in [1, 2]:
          with self.subTest(i=i):
            for j in [2, 3]:
              with self.subTest(j=j):
                if j == 2:
                  self.fail('failure')

    result = MockTestResult()
    Foo().run(result)

    # The first layer subtest result is only added to the output when it is a
    # success
    expected_success = [{'i': 1, 'j': 3}, {'i': 2, 'j': 3}]
    expected_failure = [{'i': 1, 'j': 2}, {'i': 2, 'j': 2}]
    self.assertListEqual(expected_success, result.subtest_success)
    self.assertListEqual(expected_failure, result.subtest_failure) 
Example #12
Source File: loanertest.py    From loaner with Apache License 2.0 6 votes vote down vote up
def setUp(self):
    """Checks imported modules for an action module and includes class."""
    super(ActionTestCase, self).setUp()
    try:
      self.testing_action
    except AttributeError:
      raise EnvironmentError(
          'Create a TestCase setUp method that sets a variable named '
          'self.testing_action containing the name of the action module you '
          'wish to test, then runs the superclass setUp method.')
    actions = action_loader.load_actions([self.testing_action])  # pylint: disable=no-member
    if not (actions.get('sync') or actions.get('async')):
      raise EnvironmentError(
          'The unit test must import at least one valid action module. Verify '
          'that self.testing_action is a string that is the name of a module '
          'in the actions directory.')
    self.action = (
        actions['sync'].get(self.testing_action) or
        actions['async'].get(self.testing_action))
    self.addCleanup(self.reset_cached_actions) 
Example #13
Source File: parameterized_test.py    From abseil-py with Apache License 2.0 5 votes vote down vote up
def test_no_duplicate_decorations(self):
    with self.assertRaises(AssertionError):

      @parameterized.parameters(1, 2, 3, 4)
      class _(parameterized.TestCase):

        @parameterized.parameters(5, 6, 7, 8)
        def test_something(self, unused_obj):
          pass 
Example #14
Source File: flagsaver_test.py    From abseil-py with Apache License 2.0 5 votes vote down vote up
def test_flag_saver_call_on_class(self):
    with self.assertRaises(TypeError):

      # WRONG. Don't do this.
      # Consider the correct usage example in FlagSaverSetUpTearDownUsageTest.
      @flagsaver.flagsaver()
      class FooTest(absltest.TestCase):

        def test_tautology(self):
          pass

      del FooTest 
Example #15
Source File: parameterized_test.py    From abseil-py with Apache License 2.0 5 votes vote down vote up
def test_addition(self, op1, op2, result):
      pass  # Always passes, but not called w/out TestCase. 
Example #16
Source File: parameterized_test.py    From abseil-py with Apache License 2.0 5 votes vote down vote up
def test_generator_tests(self):
    with self.assertRaises(AssertionError):

      # This fails because the generated test methods share the same test id.
      class GeneratorTests(parameterized.TestCase):
        test_generator_method = (lambda self: None for _ in range(10))

      del GeneratorTests 
Example #17
Source File: parameterized_test.py    From abseil-py with Apache License 2.0 5 votes vote down vote up
def test_duplicate_dict_named_test_fails(self):
    with self.assertRaises(parameterized.DuplicateTestNameError):

      class _(parameterized.TestCase):

        @parameterized.named_parameters(
            {'testcase_name': 'Interesting', 'unused_obj': 0},
            {'testcase_name': 'Interesting', 'unused_obj': 1},
        )
        def test_dict_something(self, unused_obj):
          pass 
Example #18
Source File: parameterized_test.py    From abseil-py with Apache License 2.0 5 votes vote down vote up
def test_duplicate_mixed_named_test_fails(self):
    with self.assertRaises(parameterized.DuplicateTestNameError):

      class _(parameterized.TestCase):

        @parameterized.named_parameters(
            {'testcase_name': 'Interesting', 'unused_obj': 0},
            ('Interesting', 1),
        )
        def test_mixed_something(self, unused_obj):
          pass 
Example #19
Source File: parameterized_test.py    From abseil-py with Apache License 2.0 5 votes vote down vote up
def test_named_test_with_no_name_fails(self):
    with self.assertRaises(RuntimeError):

      class _(parameterized.TestCase):

        @parameterized.named_parameters(
            (0,),
        )
        def test_something(self, unused_obj):
          pass 
Example #20
Source File: parameterized_test.py    From abseil-py with Apache License 2.0 5 votes vote down vote up
def test_named_test_dict_with_no_name_fails(self):
    with self.assertRaises(RuntimeError):

      class _(parameterized.TestCase):

        @parameterized.named_parameters(
            {'unused_obj': 0},
        )
        def test_something(self, unused_obj):
          pass 
Example #21
Source File: absltest_test.py    From abseil-py with Apache License 2.0 5 votes vote down vote up
def test_assert_items_equal_hotfix(self):
    """Confirm that http://bugs.python.org/issue14832 - b/10038517 is gone."""
    for assert_items_method in (self.assertItemsEqual, self.assertCountEqual):
      with self.assertRaises(self.failureException) as error_context:
        assert_items_method([4], [2])
      error_message = str(error_context.exception)
      # Confirm that the bug is either no longer present in Python or that our
      # assertItemsEqual patching version of the method in absltest.TestCase
      # doesn't get used.
      self.assertIn('First has 1, Second has 0:  4', error_message)
      self.assertIn('First has 0, Second has 1:  2', error_message) 
Example #22
Source File: absltest_test.py    From abseil-py with Apache License 2.0 5 votes vote down vote up
def test_assert_dict_contains_subset(self):
    self.assertDictContainsSubset({}, {})

    self.assertDictContainsSubset({}, {'a': 1})

    self.assertDictContainsSubset({'a': 1}, {'a': 1})

    self.assertDictContainsSubset({'a': 1}, {'a': 1, 'b': 2})

    self.assertDictContainsSubset({'a': 1, 'b': 2}, {'a': 1, 'b': 2})

    self.assertRaises(absltest.TestCase.failureException,
                      self.assertDictContainsSubset, {'a': 2}, {'a': 1},
                      '.*Mismatched values:.*')

    self.assertRaises(absltest.TestCase.failureException,
                      self.assertDictContainsSubset, {'c': 1}, {'a': 1},
                      '.*Missing:.*')

    self.assertRaises(absltest.TestCase.failureException,
                      self.assertDictContainsSubset, {'a': 1, 'c': 1}, {'a': 1},
                      '.*Missing:.*')

    self.assertRaises(absltest.TestCase.failureException,
                      self.assertDictContainsSubset, {'a': 1, 'c': 1}, {'a': 1},
                      '.*Missing:.*Mismatched values:.*') 
Example #23
Source File: absltest_test.py    From abseil-py with Apache License 2.0 5 votes vote down vote up
def test_assert_multi_line_equal(self):
    sample_text = """\
http://www.python.org/doc/2.3/lib/module-unittest.html
test case
    A test case is the smallest unit of testing. [...]
"""
    revised_sample_text = """\
http://www.python.org/doc/2.4.1/lib/module-unittest.html
test case
    A test case is the smallest unit of testing. [...] You may provide your
    own implementation that does not subclass from TestCase, of course.
"""
    sample_text_error = """
- http://www.python.org/doc/2.3/lib/module-unittest.html
?                             ^
+ http://www.python.org/doc/2.4.1/lib/module-unittest.html
?                             ^^^
  test case
-     A test case is the smallest unit of testing. [...]
+     A test case is the smallest unit of testing. [...] You may provide your
?                                                       +++++++++++++++++++++
+     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.assertRaises(AssertionError, self.assertMultiLineEqual, (1, 2), 'str')
    self.assertRaises(AssertionError, self.assertMultiLineEqual, 'str', (1, 2)) 
Example #24
Source File: unittest3_backport_test.py    From abseil-py with Apache License 2.0 5 votes vote down vote up
def test_subtest_unexpected_success(self):
    class Foo(absltest.TestCase):

      @unittest.expectedFailure
      def runTest(self):
        for i in [1, 2, 3]:
          with self.subTest(i=i):
            self.assertEqual(i, i)

    foo = Foo()
    with mock.patch.object(foo, '_addUnexpectedSuccess',
                           autospec=True) as mock_subtest_unexpected_success:
      result = MockTestResult()
      foo.run(result)
      self.assertEqual(mock_subtest_unexpected_success.call_count, 1) 
Example #25
Source File: unittest3_backport_test.py    From abseil-py with Apache License 2.0 5 votes vote down vote up
def test_subtest_expected_failure(self):
    class Foo(absltest.TestCase):

      @unittest.expectedFailure
      def runTest(self):
        for i in [1, 2, 3]:
          with self.subTest(i=i):
            self.assertEqual(i, 2)

    foo = Foo()
    with mock.patch.object(foo, '_addExpectedFailure',
                           autospec=True) as mock_subtest_expected_failure:
      result = MockTestResult()
      foo.run(result)
      self.assertEqual(mock_subtest_expected_failure.call_count, 1) 
Example #26
Source File: absltest_test.py    From abseil-py with Apache License 2.0 5 votes vote down vote up
def test_subclass(self):

    class Subclass(absltest.TestCase):

      def __init__(self):  # pylint: disable=super-init-not-called
        pass

    Subclass().assertEquals({}, {}) 
Example #27
Source File: utils.py    From pysc2 with Apache License 2.0 5 votes vote down vote up
def setUp(self):
    super(TestCase, self).setUp()
    stopwatch.sw.clear()
    stopwatch.sw.enable() 
Example #28
Source File: loanertest.py    From loaner with Apache License 2.0 5 votes vote down vote up
def tearDown(self):
    """Tear down the testing environment."""
    self.logout_user()
    self.testbed.deactivate()
    super(TestCase, self).tearDown() 
Example #29
Source File: test_util.py    From data-validation with Apache License 2.0 5 votes vote down vote up
def make_example_dict_equal_fn(
    test: absltest.TestCase,
    expected: List[types.Example]) -> Callable[[List[types.Example]], None]:
  """Makes a matcher function for comparing the example dict.

  Args:
    test: test case object.
    expected: the expected example dict.

  Returns:
    A matcher function for comparing the example dicts.
  """
  def _matcher(actual):
    """Matcher function for comparing the example dicts."""
    try:
      # Check number of examples.
      test.assertLen(actual, len(expected))
      for i in range(len(actual)):
        for key in actual[i]:
          # Check each feature value.
          if isinstance(expected[i][key], np.ndarray):
            test.assertEqual(
                expected[i][key].dtype, actual[i][key].dtype,
                'Expected dtype {}, found {} in actual[{}][{}]: {}'.format(
                    expected[i][key].dtype, actual[i][key].dtype, i, key,
                    actual[i][key]))
            np.testing.assert_equal(actual[i][key], expected[i][key])
          else:
            test.assertEqual(
                expected[i][key], actual[i][key],
                'Unexpected value of actual[{}][{}]'.format(i, key))

    except AssertionError:
      raise util.BeamAssertException(traceback.format_exc())

  return _matcher 
Example #30
Source File: test_util.py    From data-validation with Apache License 2.0 5 votes vote down vote up
def make_dataset_feature_stats_list_proto_equal_fn(
    test: absltest.TestCase,
    expected_result: statistics_pb2.DatasetFeatureStatisticsList
) -> Callable[[List[statistics_pb2.DatasetFeatureStatisticsList]], None]:
  """Makes a matcher function for comparing DatasetFeatureStatisticsList proto.

  Args:
    test: test case object
    expected_result: the expected DatasetFeatureStatisticsList proto.

  Returns:
    A matcher function for comparing DatasetFeatureStatisticsList proto.
  """

  def _matcher(actual: List[statistics_pb2.DatasetFeatureStatisticsList]):
    """Matcher function for comparing DatasetFeatureStatisticsList proto."""
    try:
      test.assertLen(actual, 1,
                     'Expected exactly one DatasetFeatureStatisticsList')
      test.assertLen(actual[0].datasets, len(expected_result.datasets))

      sorted_actual_datasets = sorted(actual[0].datasets, key=lambda d: d.name)
      sorted_expected_datasets = sorted(expected_result.datasets,
                                        key=lambda d: d.name)

      for i in range(len(sorted_actual_datasets)):
        assert_dataset_feature_stats_proto_equal(test,
                                                 sorted_actual_datasets[i],
                                                 sorted_expected_datasets[i])
    except AssertionError:
      raise util.BeamAssertException(traceback.format_exc())

  return _matcher