Skip to content

Latest commit

 

History

History
92 lines (69 loc) · 4.33 KB

TestCombinations.md

File metadata and controls

92 lines (69 loc) · 4.33 KB

Testing Combinations

Contents

When to use Combinations

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.

Steps

  1. Copy this starter text, and adjust for the number of inputs that you have.

String[] inputs1 = {"input1.value1", "input1.value2"};
String[] inputs2 = {"input2.value1", "input2.value2", "input2.value3"};
CombinationApprovals.verifyAllCombinations((a, b) -> "placeholder", inputs1, inputs2);

snippet source | anchor

  1. Modify each input container for your chosen values.
  2. 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:

[input1.value1, input2.value1] => placeholder 
[input1.value1, input2.value2] => placeholder 
[input1.value1, input2.value3] => placeholder 
[input1.value2, input2.value1] => placeholder 
[input1.value2, input2.value2] => placeholder 
[input1.value2, input2.value3] => placeholder

snippet source | anchor

  1. Implement the body of your lambda
  2. Run it, and approve the output.

The Basics

You can use CombinationApprovals.verifyAllCombinations to test the content of multiple containers.

This makes a kind of approval test matrix, automatically testing all combinations of a set of inputs. It's a powerful way to quickly get very good test coverage.

In this small example, all combinations of {"hello", "world"} and {1, 2, 3} are being used:

String[] strings = {"hello", "world"};
Integer[] numbers = {1, 2, 3};
CombinationApprovals.verifyAllCombinations((s, i) -> String.format("(%s,%s)", s, i), strings, numbers);

snippet source | anchor

The format is carefully chosen to show both inputs and outputs, to make the test results easy to interpret. The output looks like this:

[hello, 1] => (hello,1) 
[hello, 2] => (hello,2) 
[hello, 3] => (hello,3) 
[world, 1] => (world,1) 
[world, 2] => (world,2) 
[world, 3] => (world,3)

snippet source | anchor

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.


Back to User Guide