If you have a function isAwake(day, time)
and you want to test it for multiple days & times.
Combination Testing can be used to generate all combinations of provided inputs.
Input1 (Day) |
Input2 (Time) |
Output (isAwake) |
---|---|---|
Sunday | 7:00 | No |
Sunday | 9:00 | No |
Sunday | 11:00 | Maybe |
Monday | 7:00 | Maybe |
Monday | 9:00 | Yes |
Monday | 11:00 | Yes |
If you wanted to test this matrix combination approvals can do it with a single line:
verify_all_combinations(is_awake, [["Monday", "Sunday"], ["7:00", "9:00", "11:00"]])
You have a function that takes, for example, 3 parameters, and you want to test its behaviour with a bunch of different values for each of those parameters.
If you have only one parameter that you want to vary, check out How to Test a Variety of Values for One Input.
- Copy this starter text, and adjust for the number of inputs that you have.
inputs1 = ["input1.value1", "input1.value2"]
inputs2 = ["input2.value1", "input2.value2", "input2.value3"]
verify_all_combinations(lambda a, b: "placeholder", [inputs1, inputs2])
- Modify each input container for your chosen values.
- Run it, and make sure that you have your inputs wired up correctly.
If they are wired up correctly, you will see a file that looks like this: it is the left hand side of the file that matters at this point: all combinations of your own input values should be listed:
args: ('input1.value1', 'input2.value1') => 'placeholder'
args: ('input1.value1', 'input2.value2') => 'placeholder'
args: ('input1.value1', 'input2.value3') => 'placeholder'
args: ('input1.value2', 'input2.value1') => 'placeholder'
args: ('input1.value2', 'input2.value2') => 'placeholder'
args: ('input1.value2', 'input2.value3') => 'placeholder'
- Implement the body of your lambda
- Run it, and approve the output.
For advice on effective formatting, see Tips for Designing Strings. As you write out larger volumes of data in your approval files, experience has shown that the choice of layout of text in approval files can make a big difference to maintainability of tests, when failures occur.
You can also use named inputs, which can make your tests more readable. Here is an example:
verify_all_combinations_with_labeled_input(
self.func,
arg1=(1, 3),
arg2=(2, 4),
)
which will produce the following output:
(arg1: 1, arg2: 2) => 4
(arg1: 1, arg2: 4) => 6
(arg1: 3, arg2: 2) => 6
(arg1: 3, arg2: 4) => 8