hbutils.testing.generator.func
Test matrix generation utilities for parameterized test cases.
This module provides a compact interface for generating parameter combinations used in test parameterization. It supports two modes of generation:
MatrixMode- Enumeration of available generation modes.tmatrix()- Generate parameter names and test case tuples for pytest.
The primary entry point is tmatrix(), which converts a parameter range
specification into a list of parameter names and a list of case tuples. These
results are compatible with pytest.mark.parametrize and can be used for
both full Cartesian matrix generation and AETG-based combinatorial testing.
Note
The AETG algorithm can reduce the number of test cases while preserving combinatorial coverage of parameter interactions.
Example:
>>> from hbutils.testing.generator.func import tmatrix
>>> names, cases = tmatrix({
... 'a': [1, 2],
... 'b': ['x', 'y'],
... ('c', 'd'): [(0, 1), (1, 2)]
... }, mode='aetg', level=2)
>>> names
['a', 'b', 'c', 'd']
>>> cases
[(1, 'x', 0, 1), ...]
__all__
- hbutils.testing.generator.func.__all__ = ['tmatrix']
Built-in mutable sequence.
If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.
MatrixMode
tmatrix
- hbutils.testing.generator.func.tmatrix(ranges: Mapping[str | Tuple[str, ...], List[Any]], mode: str | MatrixMode = 'aetg', seed: int | None = 0, level: int = 2) Tuple[List[str], List[Tuple[Any, ...]]][source]
Generate test matrix for parameterized testing.
This function creates a test matrix that can be directly used with pytest’s parametrize decorator. It supports two generation modes: AETG for efficient combinatorial testing and MATRIX for full Cartesian product generation.
- Parameters:
ranges (Mapping[Union[str, Tuple[str, ...]], List[Any]]) – Mapping of parameter names to their possible values. Keys can be either a single string (for one parameter) or a tuple of strings (for multiple parameters that should be varied together). Values are lists of possible values for the parameter(s).
mode (Union[str, MatrixMode]) – Generation mode, accepts
'aetg'/'matrix'(case-insensitive) or aMatrixModeinstance. Default is'aetg'.seed (Optional[int]) – Random seed for AETG mode. Default is
0which produces deterministic results. Set toNonefor non-deterministic generation.level (int) – Coverage level for AETG algorithm, indicating the strength of combinatorial coverage (e.g.,
2for pairwise coverage). Default is2.
- Returns:
A tuple containing
(parameter_names, test_cases)whereparameter_namesis a list of parameter names andtest_casesis a list of tuples, each representing one test case.- Return type:
Tuple[List[str], List[Tuple[Any, …]]]
- Raises:
ValueError – If an invalid mode is specified.
- Examples::
>>> from hbutils.testing import tmatrix >>> names, values = tmatrix( ... { ... 'a': [2, 3], ... 'e': ['a', 'b', 'c'], ... ('b', 'c'): [(1, 7), (4, 6), (9, 12)], ... } ... ) >>> print(names) ['a', 'e', 'b', 'c'] >>> for i, v in enumerate(values): ... print(i, v) 0 (2, 'c', 9, 12) 1 (3, 'c', 4, 6) 2 (2, 'c', 1, 7) 3 (3, 'b', 9, 12) 4 (2, 'b', 4, 6) 5 (3, 'b', 1, 7) 6 (3, 'a', 9, 12) 7 (2, 'a', 4, 6) 8 (3, 'a', 1, 7)
Note
This can be directly used in
pytest.mark.parametrizefunction.>>> import pytest >>> @pytest.mark.unittest ... class TestTestingGeneratorFunc: ... @pytest.mark.parametrize(*tmatrix({ ... 'a': [2, 3], ... 'e': ['a', 'b', 'c'], ... ('b', 'c'): [(1, 7), (4, 6), (9, 12)], ... })) ... def test_tmatrix_usage(self, a, e, b, c): ... print(a, e, b, c)