Skip to content

Conversation

@copybara-service
Copy link

@copybara-service copybara-service bot commented Oct 28, 2025

Add a @parameterized.named_product which combines both named_parameters and product together.

Named Parameterized Test Cases of a Cartesian Product

Both named_parameters and product have useful features.
However, when using both, it can be difficult to ensure that generated test
cases retain useful names.

Here, we combine both approaches to create parameterized tests with both
generated permutations and human-readable names.

Example:

    @parameterized.named_product(
        [
          dict(
              testcase_name='five_mod_three_is_2',
              num=5,
              modulo=3,
              expected=2,
          ),
          dict(
              testcase_name='seven_mod_four_is_3',
              num=7,
              modulo=4,
              expected=3,
          ),
        ],
        [
            dict(testcase_name='int', dtype=int),
            dict(testcase_name='float', dtype=float),
        ]
    )
    def testModuloResult(self, num, modulo, expected, dtype):
      self.assertEqual(expected, dtype(num) % modulo)

  This would generate the test cases:

    testModuloResult_five_mod_three_is_2_int
    testModuloResult_five_mod_three_is_2_float
    testModuloResult_seven_mod_four_is_3_int
    testModuloResult_seven_mod_four_is_3_float

…eters` and `product` together.

Named Parameterized Test Cases of a Cartesian Product
======================================================

Both `named_parameters` and `product` have useful features.
However, when using both, it can be difficult to ensure that generated test
cases retain useful names.

Here, we combine both approaches to create parameterized tests with both
generated permutations and human-readable names.

Example:
```python
    @parameterized.named_product(
        [
          dict(
              testcase_name='five_mod_three_is_2',
              num=5,
              modulo=3,
              expected=2,
          ),
          dict(
              testcase_name='seven_mod_four_is_3',
              num=7,
              modulo=4,
              expected=3,
          ),
        ],
        [
            dict(testcase_name='int', dtype=int),
            dict(testcase_name='float', dtype=float),
        ]
    )
    def testModuloResult(self, num, modulo, expected, dtype):
      self.assertEqual(expected, dtype(num) % modulo)

  This would generate the test cases:

    testModuloResult_five_mod_three_is_2_int
    testModuloResult_five_mod_three_is_2_float
    testModuloResult_seven_mod_four_is_3_int
    testModuloResult_seven_mod_four_is_3_float
```

PiperOrigin-RevId: 824463104
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant