hbutils.testing.generator

BaseGenerator

class hbutils.testing.generator.BaseGenerator(values, names: Optional[List[str]] = None)[source]

Base generator class.

__init__(values, names: Optional[List[str]] = None)[source]

Constructor of the hbutils.testing.BaseGenerator class.

Parameters:
  • values – Selection values, such as {'a': [2, 3], 'b': ['b', 'c']}.

  • names – Names of the given generator, default is None which means use the sorted key set of the values.

cases() → Iterator[Mapping[str, object]][source]

Virtual method for cases, will be implemented in child classes.

property names

Name of the given generator.

tuple_cases() → Iterator[Tuple[object, ]][source]

Tuple-formatted cases, can be used to iterate.

property values

Selection values.

MatrixGenerator

class hbutils.testing.generator.MatrixGenerator(values: Mapping[str, Any], names: Optional[List[str]] = None, includes: Optional[List[Mapping[str, Any]]] = None, excludes: Optional[List[Mapping[str, Any]]] = None)[source]

Full matrix model, all the cases in this matrix will be iterated.

__init__(values: Mapping[str, Any], names: Optional[List[str]] = None, includes: Optional[List[Mapping[str, Any]]] = None, excludes: Optional[List[Mapping[str, Any]]] = None)[source]

Constructor of the hbutils.testing.MatrixGenerator class. It is similar to GitHub Action’s matrix.

Parameters:
  • values – Matrix values, such as {'a': [2, 3], 'b': ['b', 'c']}.

  • names – Names of the given generator, default is None which means use the sorted key set of the values.

  • includes – Include items, such as [{'a': 4, 'b': 'b'}], default is None which means no extra inclusions.

  • excludes – Exclude Items, such as [{'a': 2, 'b': 'c'}], default is None which means no extra exclusions.

cases() → Iterator[Mapping[str, Any]][source]

Get the cases in this matrix.

Examples::
>>> from hbutils.testing import MatrixGenerator
>>> for p in MatrixGenerator(
...         {'a': [1, 2, 3], 'b': ['a', 'b'], 'r': [3, 4, 5]},
...         includes=[{'a': 4, 'r': 7}],
...         excludes=[{'a': 1, 'r': 3}, {'a': 3, 'b': 'b'}, {'a': 4, 'b': 'a', 'r': 7}]
... ).cases():
>>>     print(p)
{'a': 1, 'b': 'a', 'r': 4}
{'a': 1, 'b': 'a', 'r': 5}
{'a': 1, 'b': 'b', 'r': 4}
{'a': 1, 'b': 'b', 'r': 5}
{'a': 2, 'b': 'a', 'r': 3}
{'a': 2, 'b': 'a', 'r': 4}
{'a': 2, 'b': 'a', 'r': 5}
{'a': 2, 'b': 'b', 'r': 3}
{'a': 2, 'b': 'b', 'r': 4}
{'a': 2, 'b': 'b', 'r': 5}
{'a': 3, 'b': 'a', 'r': 3}
{'a': 3, 'b': 'a', 'r': 4}
{'a': 3, 'b': 'a', 'r': 5}
{'a': 4, 'b': 'b', 'r': 7}
property excludes

Exclude items.

property includes

Include items.

property names

Name of the given generator.

tuple_cases() → Iterator[Tuple[object, ]]

Tuple-formatted cases, can be used to iterate.

property values

Selection values.

AETGGenerator

class hbutils.testing.generator.AETGGenerator(values, names: Optional[List[str]] = None, pairs: Optional[List[Tuple[str, ]]] = None, rnd: Optional[Union[random.Random, int]] = None)[source]

Full AETG model, test cases will be generated to make sure the required pairs will be all tested.

__init__(values, names: Optional[List[str]] = None, pairs: Optional[List[Tuple[str, ]]] = None, rnd: Optional[Union[random.Random, int]] = None)[source]

Constructor of the hbutils.testing.AETGGenerator class.

Parameters:
  • values – Selection values, such as {'a': [2, 3], 'b': ['b', 'c']}.

  • names – Names of the given generator, default is None which means use the sorted key set of the values.

  • pairs – Pairs required to be all tested, default is None which means all the binary pairs will be included.

  • rnd – Random object or int-formatted random seed, default is None which means the default random object will be used.

cases() → Iterator[Mapping[str, object]][source]

Get the cases in this AETG model.

Examples::
>>> from hbutils.testing import AETGGenerator
>>> gene = AETGGenerator({'a': (1, 2), 'b': (3, 4), 'c': (5, 6), 'd': (7, 8), 'e': (9, 10)})
>>> for p in gene.cases():
...     print(p)
{'a': 1, 'b': 3, 'c': 6, 'd': 8, 'e': 10}
{'a': 2, 'b': 4, 'c': 5, 'd': 7, 'e': 9}
{'a': 2, 'b': 3, 'c': 6, 'd': 7, 'e': 9}
{'a': 2, 'b': 4, 'c': 6, 'd': 8, 'e': 10}
{'a': 1, 'b': 3, 'c': 5, 'd': 7, 'e': 10}
{'a': 1, 'b': 4, 'c': 5, 'd': 8, 'e': 9}
>>> gene = AETGGenerator(
...     {'a': (1, 2), 'b': (3, 4), 'c': (5, 6), 'd': (7, 8), 'e': (9, 10)},
...     pairs=[('a', 'c'), ('b', 'd'), ('e',)]
... )
>>> for p in gene.cases():
...     print(p)
{'a': 2, 'b': 3, 'c': 6, 'd': 8, 'e': 9}
{'a': 1, 'b': 4, 'c': 5, 'd': 7, 'e': 10}
{'a': 2, 'b': 4, 'c': 5, 'd': 8, 'e': 9}
{'a': 1, 'b': 3, 'c': 6, 'd': 7, 'e': 10}
property names

Name of the given generator.

property pairs

Pairs required to be all tested.

tuple_cases() → Iterator[Tuple[object, ]]

Tuple-formatted cases, can be used to iterate.

property values

Selection values.

tmatrix

hbutils.testing.generator.tmatrix(ranges: Mapping[Union[str, Tuple[str, ]], List], mode='aetg', seed: Optional[int] = 0, level: int = 2) → Tuple[List[str], List[Tuple]][source]
Overview:

Test matrix generator, which can be used in pytest.mark.parameterize.

Parameters:
  • ranges – Ranges of the values

  • mode – Mode of the matrix, should be one of the aetg or matrix. Default is aetg.

  • seed – Random seed, default is 0 which means the result is fixed (recommended).

  • level – Lavel of AETG generating algorithm, default is 2.

Returns:

A tuple - (names, values).

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.parametrize function.

>>> @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)