To test your code with different parameter combinations, you can use a table of values as input for your test cases. This is called data driven testing also known as table driven testing.
forNone function, passing in one or more
row objects, where each row object contains
the values to be used be a single invocation of the test. After the
forNone function, setup your
actual test function to accept the values of each row as parameters.
The row object accepts any set of types, and the type checker will ensure your types are consistent with the parameter types in the test function.
In the above example, the
square parameters are automatically inferred to be integers.
If there is an error for any particular input row, then the test will fail and KotlinTest will automatically
match up each input to the corresponding parameter names. For example, if we change the previous example to include the row
then the test will be marked as a failure with the following error message.
Table testing can be used within any spec. Here is an example using
It may be desirable to have each row of data parameters as an individual test. To generating such individual tests follow a similar pattern for each spec style. An example in the
FreeSpec is below.
Produces 4 tests and 2 parent descriptions: