Combinatorial Tests
✋ Available in version >=
2.1.2
Combinatorial testing is supported by allowing you to define tests that can take arguments, as well as allowing you to easily define tests that check different combinations using TestSketches.
In addition, a convenient collection of tools used for combinatorial testing is provided including calculation of Covering Arrays for pairwise and n-wise testing using the IPOG algorithm.
Let’s see the basic application of combinatorial tests to testing a simple pressure switch
defined by the pressure_switch function below, where it has a fault when
pressure < 10 or pressure > 30, and volume > 250.
1 | def pressure_switch(pressure, volume): |
Simple Approach (Nested For-loops)
We can check all the combinations, including some extra values, using the following
TestScenario that uses nested for-loops to iterate over each combination of
pressure and volume values to check our Example: Pressure Switch.
1 |
|
As expected it will catch the fault in the following combinations:
1 | Failing |
Computed Combinations (Cartesian Product)
Another approach to using nested for-loops is to compute all the combinations
using the Cartesian Product function. Here is the
TestScenario that uses the product(*iterables, repeat=1) function
to compute all combinations of pressure and volume values to check our Example: Pressure Switch.
The advantage of using the cartesian product function is that it avoids writing nested for-loops and is much more scalable when the number of variables is large.
1 | from testflows.combinatorics import product |
Using Sketches
A simple approach to check all possible combinations is to use a TestSketch.
✋ Sketches currently do not support filtering or Covering Arrays. See Filtering Combinations and Covering Array Combinations for more details.
A TestSketch allows you to check all possible combinations, where each combination variable and its values are defined by the either() function. TestSketch with either() function makes writing combinatorial tests as simple as writing a simple test that would check one combination.
✋ If you call the either() function multiple times on the same line of code or you have a call to the either() function inside a
for-looporwhile-loop, then unique identifierimust be specified explicitly.
For the Example: Pressure Switch, our TestSketch would be as follows:
1 |
|
Each either() function defines a new combination variable and its possible values, and then TestFlows automatically loops through all the combinations when a TestSketch is called.
Running the TestSketch above results in the following output:
1 | Sep 22,2023 16:29:07 ⟥ Scenario check pressure switch, flags:TE |
Fails are reported as below.
1 | Failing |
A TestSketch allows for an advanced and intuitive definition of combinatorial tests especially when the number of combination variables grows.
Each call to the either() function must be unique, or a unique identifier i must be specified.
By default, the unique identifier of the either() function is the source code line number;
therefore, if you call the either() function multiple times on the same line of code
or you have a call to the either() function inside a for-loop or while-loop,
then the unique identifier i must be specified explicitly.
For example, let’s assume we have a function add(a, b, c) that we want to test.
1 | def add(a, b, c): |
We could write a TestSketch that calls the either() function inside a for-loop
on the same line multiple times as follows:
1 |
|
Note that we had to pass a unique identifier i for each either() function call that defined possible
values of a, b, and c. Also, note that the unique identifier was a combination of the variable name
and the loop variable i.
1 | add(a=either(1, 2, i=f"a{i}"), b=either(3, 4, i=f"b{i}"), c=either(5, 6, i=f"c{i}")) |
The TestSketch above results in the generation of eight combinations. This is because
range(1,2) is [1], and therefore the for-loop has only one iteration.
1 | for i in range(either(value=range(1, 2))): |
1 | Sep 22,2023 17:00:48 ⟥ Scenario check add |
Let’s change the for-loop so that it can iterate either one or two times.
1 |
|
Then the number of combinations will be 72, and it will not be that intuitive what each combination is.
The first run of the for-loop, where it iterates only once, will produce the same 8 combinations
as before. However, the second run of the for-loop, when it iterates twice, will
create combinations similar to the ones below. The combination 1 + 3 + 5
is followed by each of the 8 possibilities, including itself, and this is done for each
combination of a + b + c, and thus resulting in 8 * 8 + 8 = 72 total combinations.
1 | Sep 22,2023 17:08:13 ⟥ Combination pattern #8 |
Randomizing Combinations
Use the random test attribute of the TestSketch to randomize the order of combinations.
For example,
1 |
|
✋ See Using either() for how to randomize order for a given combination variable.
You can combine random with the limit test attribute. See the Limiting Number of Combinations section below.
Also, you can use the Args decorator to specify the default value of the random test attribute.
For example,
1 |
|
Limiting the Number of Combinations
Use the limit test attribute of the TestSketch to limit the number of combinations.
For example,
1 |
|
✋ See Using either() for how to limit the number of values for a given combination variable.
Use both the random and limit attributes of the TestSketch to execute a limited number of random
combinations.
For example,
1 | # Run the sketch only for the first 5 random combinations |
You can also use the Args decorator to specify the default value of the limit test attribute.
1 |
|
Using either()
The either() function selects values for a combination variable one at a time until all values are consumed.
✋ This function must be called only once for each line of code in the same source file, or a unique identifier
imust be specified.
✋ It is used in TestSketches to check all possible combinations. See Using Sketches.
Values can be specified either using *values or by passing an iterator or generator
as a value.
If neither *values nor value is explicitly specified, then *values
is set to a (True, False) tuple.
If random is True, then all values will be shuffled using the default shuffle function.
Optionally, you can pass a custom shuffle function that takes values as an argument
and modifies the sequence in place. By default, random.shuffle() is used.
You can use limit to limit the number of values to choose.
1 | either(*values, value=None, i=None, random=False, shuffle=random.shuffle, limit=None) |
where
*valueszero or more of values to choose fromvalueiterator or generator of valuesrandom(optional) randomize order of values (values must fit into memory), default:Falseshuffle(optional) custom function to shuffle the valueslimit(optional) limit number of values (integer> 0), default:Nonei(optional) unique identifier, default:None
Using Combination Outlines
TestSketch provides a very easy way to define a combinatorial test. However, Sketches do have their limitations, and sometimes using a Combination Outline is necessary.
1 |
Here is an example of using TestSketch for testing a few combinations of a calculator’s
+, -, *, and / operations when the user enters some positive or negative numbers,
and presses the = sign to get the results.
1 |
|
Let’s now implement the TestSketch above using a Combination Outline and the product() function
to see the difference between them.
1 |
|
As can be seen above, using an Outline is more involved and forces us to create a named variable for each combination variable and compute each combination explicitly using the cartesian product() function that produces combinations that need to be passed to the Outline, which then has to unpack the combination into its individual combination variables to be used in its test procedure.
However, a combination outline can provide more control over how a test iterates over each combination, allowing the possibility of filtering invalid combinations as well as the ability to use them with Covering Arrays.
| Feature | Sketch | Combination Outline |
|---|---|---|
| Ease of use | Easy | Moderate |
| Combination Filtering | No | Yes |
| Covering Arrays | No | Yes |
Filtering Combinations
Use a Combination Outline to filter invalid combinations.
For example,
1 |
|
Covering Array Combinations
Use a Combination Outline to generate a limited number of combinations using a Covering Array.
For example, with a CoveringArray(strength=3), we will have to only check 37 combinations
instead of all the 144 (2*3*4*2*3) combinations if we do not use a Covering Array, and instead do exhaustive testing.
1 |
|
Covering Arrays - (Pairwise, N-wise) Testing
The CoveringArray class allows you to calculate a covering array
for some k parameters having the same or different number of possible values.
The class uses IPOG, an in-parameter-order algorithm as described in IPOG: A General Strategy for T-Way Software Testing by Yu Lei et al.
For any non-trivial number of parameters, exhaustively testing all possibilities is not feasible.
For example, if we have 10 parameters (k=10) that each have 10 possible values (v=10), the
number of all possibilities is , thus requiring 10 billion tests for complete coverage.
Given that exhaustive testing might not be practical, a covering array could give us a much smaller
number of tests if we choose to check all possible interactions only between some fixed number
of parameters at least once, where an interaction is some specific combination, where order does not matter,
of some t number of parameters, covering all possible values that each selected parameter could have.
✋ You can find out more about covering arrays by visiting the US National Institute of Standards and Technology’s (NIST) Introduction to Covering Arrays page.
The CoveringArray(parameters, strength=2) takes the following arguments:
where,
parametersspecifies parameter names and their possible values and is specified as adict[str, list[value]], where key is the parameter name and value is a list of possible values for a given parameter.strengthspecifies the strengthtof the covering array that indicates the number of parameters in each combination, for which all possible interactions will be checked. Ifstrengthequals the number of parameters, then you get the exhaustive case.
The return value of the CoveringArray(parameters, strength=2) is a CoveringArray object that is an iterable
of tests, where each test is a dictionary, with each key being the parameter name and its value
being the parameter value.
For example,
1 | from testflows.combinatorics import CoveringArray |
Gives the following output:
1 | CoveringArray({'a': [0, 1], 'b': ['a', 'b'], 'c': [0, 1, 2], 'd': ['d0', 'd1']},2)[ |
Given that in the example above, the strength=2, all possible 2-way (pairwise)
combinations of parameters a, b, c, and d are the following:
1 | [('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd'), ('c', 'd')] |
The six tests that make up the covering array cover all the possible interactions
between the values of each of these parameter combinations. For example, the ('a', 'b')
parameter combination covers all possible combinations of the values that
parameters a and b can take.
Given that parameter a can have values [0, 1], and parameter b can have values ['a', 'b']
all possible interactions are the following:
1 | [(0, 'a'), (0, 'b'), (1, 'a'), (1, 'b')] |
where the first element of each tuple corresponds to the value of the parameter a, and the second
element corresponds to the value of the parameter b.
Examining the covering array above, we can see that all possible interactions of parameters
a and b are indeed covered at least once. The same check can be done for other parameter combinations.
Checking Covering Array
The CoveringArray.check() function can be used to verify that the tests
inside the covering array cover all possible t-way interactions at least once and thus
meet the definition of a covering array.
For example,
1 | from testflows.combinatorics import CoveringArray |
Dumping Covering Array
The CoveringArray object implements a custom __str__ method, and therefore it can be easily converted into
a string representation similar to the format used in the NIST covering array tables.
For example,
1 | print(CoveringArray(parameters, strength=2)) |
1 | CoveringArray({'a': [0, 1], 'b': ['a', 'b'], 'c': [0, 1, 2], 'd': ['d0', 'd1']},2)[ |
Combinations
The combinations(iterable, r, with_replacement=False) function can be used to calculate
all the r-length combinations of elements in a specified iterable.
For example,
1 | from testflows.combinatorics import combinations |
1 | [('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd'), ('c', 'd')] |
This function is equivalent to the standard library’s itertools.combinations.
Combinations With Replacement
You can calculate all combinations with replacement by setting the with_replacement argument to True.
For example,
1 | from testflows.combinatorics import combinations |
1 | [('a', 'a'), ('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'b'), ('b', 'c'), ('b', 'd'), ('c', 'c'), ('c', 'd'), ('d', 'd')] |
The
with_replacement=Trueoption is equivalent to the standard library’s itertools.combinations_with_replacement.
Cartesian Product
You can calculate all possible combinations of elements from different iterables using
the cartesian product(*iterables, repeat=1) function.
For example,
1 | from testflows.combinatorics import * |
1 | [(0, 'a'), (0, 'b'), (1, 'a'), (1, 'b')] |
This function is equivalent to the standard library’s itertools.product.
Permutations
The permutations(iterable, r=None) function can be used to calculate
the r-length permutations of elements for a given iterable.
✋ Permutations are different from
combinations. In a combination, the elements don’t have any order, but in a permutation, the order of elements is important.
For example,
1 | from testflows.combinatorics import * |
1 | ('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'a'), ('b', 'c'), ('b', 'd'), ('c', 'a'), ('c', 'b'), ('c', 'd'), ('d', 'a'), ('d', 'b'), ('d', 'c')] |
As we can see, both ('a', 'b') and ('b', 'a') elements are present.
This function is equivalent to the standard library’s itertools.permutations.
Binomial Coefficients
You can calculate the binomial coefficient, which is the same as
the number of ways to choose k items from n items without repetition and without order.
Binomial coefficient is defined as
when , and is zero when .
For example,
1 | from testflows.combinatorics import * |
1 | 6 |
which means that there are 6 ways to choose 2 elements out of 4.
This function is equivalent to the standard library’s math.comb.