Testing Best Practices

Testing is crucial for developing robust and safe code. Nonetheless, writing automated test is difficult and a time consuming process. Here is short summary of things you may want to consider before you start writing tests.

  • Keep functions as small as possible. Functions should deal with one single thing, and they should deal with it good. For instance consider separating data wrangling from plotting or modeling.
  • Make some sanity checks within function (e.g. check if the input is numeric). Test if your function throws errors, messages or warnings as expected.
  • Consider using more than two different data sets/vectors/files in your tests.
  • Remember to check behaviour of functions with edge cases. For instance if your function calculates 1/(x-2), then one of the tests should be to use x == 2.
  • Always perform at least one test for output structure.
  • If you depend on external data, or data used for test requires lengthy computation, use mockups.
  • Avoid unnecessary testing. If you write a simple wrapper that uses only well tested functions from other (or your own) package, the tests might be omitted.
  • For functions for which we cannot have automated test, write short testing protocol (what/how should be tested).
  • Treat you tests as an additional documentation that explains what is allowed or disallowed for inputs and what is the structure of output.
  • Every time you fix a bug, add new test covering the bug to assure that the problem won’t raise again.