R has three well-developed unit testing packages, RUnit
, svUnit
, and testthat
. Base R has examples built into its package functionality, which will return an error if they do not parse properly.
The opinion of those whom I trust is that unit testing is better than writing lots of examples, but I can't quite put my finger on any specific functionality in unit tests that can't be replicated in examples.
What features of using a unit testing framework in R make it superior to the ad hoc equivalent using package examples?
For those not from the R world, note that examples for every function in a package are run each time the package is built, and the programmer is made to suffer for any warnings or errors.
With a unit test framework you can test all kinds of things that you may not want to expose as examples to the end user:
Another advantage of unit test frameworks is speed:
My typical packages would contain dozens, if not hundreds of tests, but only a few examples that really demonstrate what the package is about.
In summary, I use tests to test, and examples to educate and help.
It of course depends which kind of examples you have, but most probably the examples are not showing how not to use the code. Unit tests can also test behaviour in negative cases.
Unit tests probably test smaller parts and therefore simpler functionalities. Tests that use frameworks can be typically run automatically after each code change. If you rely on someone running the examples all the time, sooner or later they are forgotten in some critical moment.
One of the obvious difference between tests and examples, is that tests can be a part of the development cycle. In test-driven development TDD each new feature begins with writing a test.
I will no detail here the TDD concept ( I hate generality and I think the topic is well developed in the literature) But If you always finish a session of coding by creating a failing test for the feature you want to implement next, it will be easier for you to pick up where you left off when you want to continue your development.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With