Ad hoc testing with F#

Working as a TA in a course called “Programming and Problem Solving”, one of my tasks is to create test suites to run against the students hand ins.

These tests are typically simple and either because I’m lazy or incredibly stupid, I usually resolve to writing my own ad hoc testing “frameworks” (as opposed to using e.g. NUnit).

In the course we use F# and as I really enjoy that language, this post will be about F#.
F# has a very nice List-module that I am utilizing.

The general idea behind all my testing is

  • Structure testcases as tuples
  • Organize testcases in lists
  • Write a higher-order test function
  • Partially apply the function to test and map it over the list of testcases

Let’s say we have a (very) simple function we want to test:

We can create some testcases for it, organized as tuples with the first element being the input and the second element being the expected result:

We can then create a higher-order test function, that will:

  • Take the function we want to test as an argument
  • Take a testcase as an argument
  • Unpack the testcase
  • Run the function under test with our input and compare the result to the expected result

We can now partially apply the test function with our function to test and map it over our testcases to obtain a list of booleans – the results of our tests

Even better, we can fold the boolean AND-operator over our testcases and find out if all tests passed!

Making it even more usable

Currently, our test “framework” will only tell us whether we passed all tests or not.
We can do better!
We would like to report failed tests, the input, the expected output and the actual result.
Let’s modify our higher-order test function.