本文整理汇总了Python中absl.testing.absltest.TestCase方法的典型用法代码示例。如果您正苦于以下问题:Python absltest.TestCase方法的具体用法?Python absltest.TestCase怎么用?Python absltest.TestCase使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类absl.testing.absltest
的用法示例。
在下文中一共展示了absltest.TestCase方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
# 需要导入模块: from absl.testing import absltest [as 别名]
# 或者: from absl.testing.absltest import TestCase [as 别名]
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)
示例2: test_logging_non_blocking_method
# 需要导入模块: from absl.testing import absltest [as 别名]
# 或者: from absl.testing.absltest import TestCase [as 别名]
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)
示例3: test_logging_blocking_method
# 需要导入模块: from absl.testing import absltest [as 别名]
# 或者: from absl.testing.absltest import TestCase [as 别名]
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)
示例4: test_check_type
# 需要导入模块: from absl.testing import absltest [as 别名]
# 或者: from absl.testing.absltest import TestCase [as 别名]
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))
示例5: _make_test_case_with_expected_failures
# 需要导入模块: from absl.testing import absltest [as 别名]
# 或者: from absl.testing.absltest import TestCase [as 别名]
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
示例6: get_temp_dir
# 需要导入模块: from absl.testing import absltest [as 别名]
# 或者: from absl.testing.absltest import TestCase [as 别名]
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
示例7: test_set_up_failure
# 需要导入模块: from absl.testing import absltest [as 别名]
# 或者: from absl.testing.absltest import TestCase [as 别名]
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!'}]}])
示例8: test_subtest_pass
# 需要导入模块: from absl.testing import absltest [as 别名]
# 或者: from absl.testing.absltest import TestCase [as 别名]
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)
示例9: test_subtest_fail
# 需要导入模块: from absl.testing import absltest [as 别名]
# 或者: from absl.testing.absltest import TestCase [as 别名]
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)
示例10: test_subtest_fail_fast
# 需要导入模块: from absl.testing import absltest [as 别名]
# 或者: from absl.testing.absltest import TestCase [as 别名]
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)
示例11: test_subtest_legacy
# 需要导入模块: from absl.testing import absltest [as 别名]
# 或者: from absl.testing.absltest import TestCase [as 别名]
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)
示例12: mjpy_and_dm
# 需要导入模块: from absl.testing import absltest [as 别名]
# 或者: from absl.testing.absltest import TestCase [as 别名]
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
示例13: tearDown
# 需要导入模块: from absl.testing import absltest [as 别名]
# 或者: from absl.testing.absltest import TestCase [as 别名]
def tearDown(self):
"""Tear down the testing environment."""
self.logout_user()
self.testbed.deactivate()
super(TestCase, self).tearDown()
示例14: make_example_dict_equal_fn
# 需要导入模块: from absl.testing import absltest [as 别名]
# 或者: from absl.testing.absltest import TestCase [as 别名]
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
示例15: make_dataset_feature_stats_list_proto_equal_fn
# 需要导入模块: from absl.testing import absltest [as 别名]
# 或者: from absl.testing.absltest import TestCase [as 别名]
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