Python absl.testing.parameterized.named_parameters() Examples

The following are 23 code examples of absl.testing.parameterized.named_parameters(). 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.parameterized , or try the search function .
Example #1
Source File: parameterized.py    From abseil-py with Apache License 2.0 6 votes vote down vote up
def named_parameters(*testcases):
  """A decorator for creating parameterized tests.

  See the module docstring for a usage example. For every parameter tuple
  passed, the first element of the tuple should be a string and will be appended
  to the name of the test method. Each parameter dict passed must have a value
  for the key "testcase_name", the string representation of that value will be
  appended to the name of the test method.

  Args:
    *testcases: Parameters for the decorated method, either a single iterable,
        or a list of tuples or dicts.

  Raises:
    NoTestsError: Raised when the decorator generates no tests.

  Returns:
     A test generator to be handled by TestGeneratorMetaclass.
  """
  return _parameter_decorator(_NAMED, testcases) 
Example #2
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 #3
Source File: actor_policy_test.py    From agents with Apache License 2.0 5 votes vote down vote up
def test_cases():
  return parameterized.named_parameters({
      'testcase_name': 'SimpleNet',
      'network_ctor': DummyActionNet,
  }, {
      'testcase_name': 'DistributionNet',
      'network_ctor': DummyActionDistributionNet,
  }) 
Example #4
Source File: mixture_agent_test.py    From agents with Apache License 2.0 5 votes vote down vote up
def test_cases():
  return parameterized.named_parameters(
      {
          'testcase_name': '_batch1_contextdim10_numagents2',
          'batch_size': 1,
          'context_dim': 10,
          'num_agents': 2,
      }, {
          'testcase_name': '_batch4_contextdim5_numagents10',
          'batch_size': 4,
          'context_dim': 5,
          'num_agents': 10,
      }) 
Example #5
Source File: neural_linucb_agent_test.py    From agents with Apache License 2.0 5 votes vote down vote up
def test_cases():
  return parameterized.named_parameters(
      {
          'testcase_name': '_batch1_contextdim10',
          'batch_size': 1,
          'context_dim': 10,
      }, {
          'testcase_name': '_batch4_contextdim5',
          'batch_size': 4,
          'context_dim': 5,
      }) 
Example #6
Source File: utils_test.py    From agents with Apache License 2.0 5 votes vote down vote up
def test_cases():
  return parameterized.named_parameters(
      {
          'testcase_name': '_batch1_contextdim10',
          'batch_size': 1,
          'context_dim': 10,
      }, {
          'testcase_name': '_batch4_contextdim5',
          'batch_size': 4,
          'context_dim': 5,
      }) 
Example #7
Source File: linear_bandit_policy_test.py    From agents with Apache License 2.0 5 votes vote down vote up
def test_cases_with_decomposition():
  return parameterized.named_parameters(
      {
          'testcase_name': 'batch1',
          'batch_size': 1,
          'use_decomposition': False
      }, {
          'testcase_name': 'batch4',
          'batch_size': 4,
          'use_decomposition': True
      }) 
Example #8
Source File: linear_bandit_policy_test.py    From agents with Apache License 2.0 5 votes vote down vote up
def test_cases():
  return parameterized.named_parameters(
      {
          'testcase_name': 'batch1UCB',
          'batch_size': 1,
          'exploration_strategy': linear_policy.ExplorationStrategy.optimistic,
      }, {
          'testcase_name': 'batch4UCB',
          'batch_size': 4,
          'exploration_strategy': linear_policy.ExplorationStrategy.optimistic,
      }) 
Example #9
Source File: neural_linucb_policy_test.py    From agents with Apache License 2.0 5 votes vote down vote up
def test_cases():
  return parameterized.named_parameters(
      {
          'testcase_name': '_batch1_numtrainsteps0',
          'batch_size': 1,
          'actions_from_reward_layer': False,
      }, {
          'testcase_name': '_batch4_numtrainsteps10',
          'batch_size': 4,
          'actions_from_reward_layer': True,
      }) 
Example #10
Source File: linalg_test.py    From agents with Apache License 2.0 5 votes vote down vote up
def cg_test_cases():
  return parameterized.named_parameters(
      {
          'testcase_name': '_n_1',
          'n': 1,
          'rhs': 1,
      }, {
          'testcase_name': '_n_10',
          'n': 10,
          'rhs': 1,
      }, {
          'testcase_name': '_n_100',
          'n': 100,
          'rhs': 5,
      }) 
Example #11
Source File: linalg_test.py    From agents with Apache License 2.0 5 votes vote down vote up
def test_cases():
  return parameterized.named_parameters(
      {
          'testcase_name': '_batch1_contextdim10',
          'batch_size': 1,
          'context_dim': 10,
      }, {
          'testcase_name': '_batch4_contextdim5',
          'batch_size': 4,
          'context_dim': 5,
      }) 
Example #12
Source File: drifting_linear_environment_test.py    From agents with Apache License 2.0 5 votes vote down vote up
def test_cases():
  return  parameterized.named_parameters(
      dict(testcase_name='_observation_[5]_action_[3]_batch_1',
           observation_shape=[5],
           action_shape=[3],
           batch_size=1,
           seed=12345),
      dict(testcase_name='_observation_[3]_action_[5]_batch_2',
           observation_shape=[3],
           action_shape=[5],
           batch_size=2,
           seed=98765),
      ) 
Example #13
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 #14
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 #15
Source File: parameterized_test.py    From abseil-py with Apache License 2.0 5 votes vote down vote up
def test_duplicate_named_test_fails(self):
    with self.assertRaises(parameterized.DuplicateTestNameError):

      class _(parameterized.TestCase):

        @parameterized.named_parameters(
            ('Interesting', 0),
            ('Interesting', 1),
        )
        def test_something(self, unused_obj):
          pass 
Example #16
Source File: parameterized_test.py    From abseil-py with Apache License 2.0 5 votes vote down vote up
def dict_decorator(key, value):
  """Sample implementation of a chained decorator.

  Sets a single field in a dict on a test with a dict parameter.
  Uses the exposed '_ParameterizedTestIter.testcases' field to
  modify arguments from previous decorators to allow decorator chains.

  Args:
    key: key to map to
    value: value to set

  Returns:
    The test decorator
  """
  def decorator(test_method):
    # If decorating result of another dict_decorator
    if isinstance(test_method, abc.Iterable):
      actual_tests = []
      for old_test in test_method.testcases:
        # each test is a ('test_suffix', dict) tuple
        new_dict = old_test[1].copy()
        new_dict[key] = value
        test_suffix = '%s_%s_%s' % (old_test[0], key, value)
        actual_tests.append((test_suffix, new_dict))

      test_method.testcases = actual_tests
      return test_method
    else:
      test_suffix = ('_%s_%s') % (key, value)
      tests_to_make = ((test_suffix, {key: value}),)
      # 'test_method' here is the original test method
      return parameterized.named_parameters(*tests_to_make)(test_method)
  return decorator 
Example #17
Source File: parameterized.py    From abseil-py with Apache License 2.0 5 votes vote down vote up
def __init__(self, test_class_name, new_test_name, original_test_name):
    super(DuplicateTestNameError, self).__init__(
        'Duplicate parameterized test name in {}: generated test name {!r} '
        '(generated from {!r}) already exists. Consider using '
        'named_parameters() to give your tests unique names and/or renaming '
        'the conflicting test method.'.format(
            test_class_name, new_test_name, original_test_name)) 
Example #18
Source File: test_case.py    From transform with Apache License 2.0 5 votes vote down vote up
def cross_with_function_handlers(parameters_list):
  """Cross named parameters with all function handlers.

  Takes a list of parameters suitable as an input to @named_parameters,
  and crosses it with the set of function handlers.
  A parameterized test function that uses this should have a parameter named
  `function_handler`.

  Args:
    parameters_list: A list of dicts.

  Returns:
    A list of dicts.
  """
  return cross_named_parameters(parameters_list, FUNCTION_HANDLERS) 
Example #19
Source File: test_case.py    From transform with Apache License 2.0 5 votes vote down vote up
def cross_named_parameters(*args):
  """Cross a list of lists of dicts suitable for @named_parameters.

  Takes a list of lists, where each list is suitable as an input to
  @named_parameters, and crosses them, forming a new name for each crossed test
  case.

  Args:
    *args: A list of lists of dicts.

  Returns:
    A list of dicts.
  """
  def _cross_test_cases(parameters_list):
    """Cross a list of test case parameters."""
    crossed_parameters = parameters_list[0].copy()
    for current_parameters in parameters_list[1:]:
      for name, value in current_parameters.items():
        if name == 'testcase_name':
          crossed_parameters[name] = '{}_{}'.format(
              crossed_parameters[name], value)
        else:
          assert name not in crossed_parameters, name
          crossed_parameters[name] = value
    return crossed_parameters
  return list(map(_cross_test_cases, itertools.product(*args))) 
Example #20
Source File: lax_numpy_test.py    From trax with Apache License 2.0 5 votes vote down vote up
def named_parameters(ls):
  """A version that allows an empty param list."""
  def noop(_):
    def wrapper(self, *args, **kwargs):
      self.skipTest("Empty parameter list")
    return wrapper
  if isinstance(ls, (list, tuple)) and not ls:
    return noop
  if isinstance(ls, itertools.chain):
    try:
      first = next(ls)
    except StopIteration:
      return noop
    else:
      ls = itertools.chain([first], ls)
  return parameterized.named_parameters(ls)


# TODO(wangpeng): Enable all disabled tests in this class 
Example #21
Source File: _utils.py    From tmppy with Apache License 2.0 5 votes vote down vote up
def multiple_named_parameters(*param_lists):
    result = param_lists[0]
    for param_list in param_lists[1:]:
        result = [(name1 + ', ' + name2, *args1, *args2)
                  for name1, *args1 in result
                  for name2, *args2 in param_list]
    return parameterized.named_parameters(*result) 
Example #22
Source File: test_trainer_utils.py    From lingvo with Apache License 2.0 4 votes vote down vote up
def MakeModelValidatorTestCase(model_classes):
  """Returns a TestCase that validates the training for `model_classes`.

  Usage in a test:

  MakeModelValidatorTestCase([path.to.module.MyRegisteredModel1,
                              path.to.module.MyRegisteredModel2])

  Args:
    model_classes: A list of model classes.
  """

  class _ModelValidator(parameterized.TestCase, test_utils.TestCase):
    """TestCase template for validating training and decoding models."""

    def TrainerBuilds(self, model):
      tmpdir = os.path.join(FLAGS.test_tmpdir, model.__name__)

      # Trainer should probably create these directories in the future.
      tf.io.gfile.makedirs(os.path.join(tmpdir, 'train'))

      model_params = model()
      cfg = model_params.Model()
      cfg.input = model_params.GetDatasetParams('Train')
      cfg.cluster.mode = 'sync'
      cfg.cluster.job = 'trainer_client'
      _ = trainer_lib.Trainer(cfg, '', tmpdir, tf_master='')

    def DecoderBuilds(self, model):
      tmpdir = os.path.join(FLAGS.test_tmpdir, model.__name__)
      tf.io.gfile.makedirs(tmpdir)

      model_params = model()
      cfg = model_params.Model()
      cfg.input = model_params.GetDatasetParams('Train')
      cfg.cluster.mode = 'sync'
      cfg.cluster.job = 'decoder'
      cfg.cluster.task = 0
      cfg.cluster.decoder.replicas = 1
      _ = trainer_lib.Decoder('train', cfg, '', tmpdir, tf_master='')

    # Each of these take about 10-20 seconds to run, to build the model and
    # execute the forward and backward pass building.
    @parameterized.named_parameters(_ModelTuples(model_classes))
    def testTrain(self, model):
      self.TrainerBuilds(model)

    @parameterized.named_parameters(_ModelTuples(model_classes))
    def testDecoder(self, model):
      self.DecoderBuilds(model)

  return _ModelValidator 
Example #23
Source File: linear_bandit_agent_test.py    From agents with Apache License 2.0 4 votes vote down vote up
def test_cases():
  return parameterized.named_parameters(
      {
          'testcase_name':
              '_batch1_contextdim10_float32',
          'batch_size':
              1,
          'context_dim':
              10,
          'exploration_policy':
              linear_agent.ExplorationPolicy.linear_ucb_policy,
          'dtype':
              tf.float32,
      }, {
          'testcase_name':
              '_batch4_contextdim5_float64_UCB',
          'batch_size':
              4,
          'context_dim':
              5,
          'exploration_policy':
              linear_agent.ExplorationPolicy.linear_ucb_policy,
          'dtype':
              tf.float64,
      }, {
          'testcase_name':
              '_batch4_contextdim5_float64_TS',
          'batch_size':
              4,
          'context_dim':
              5,
          'exploration_policy':
              linear_agent.ExplorationPolicy.linear_thompson_sampling_policy,
          'dtype':
              tf.float64,
      }, {
          'testcase_name':
              '_batch4_contextdim5_float64_decomp',
          'batch_size':
              4,
          'context_dim':
              5,
          'exploration_policy':
              linear_agent.ExplorationPolicy.linear_ucb_policy,
          'dtype':
              tf.float64,
          'use_eigendecomp':
              True,
      })