-
Notifications
You must be signed in to change notification settings - Fork 43
Testing R scripts
NGS R-scripts are located in https://github.com/molgenis/ngs-utils/tree/master/scripts and should be accompanied by at least a manual test; larger projects may be tested automatically, as described below.
Manual tests of the R-scripts may be implemented as we have done in the case of the NIPT pipeline. There, we have put the tests in a subdirectory, called 'test'. For each script 'X', there is a subdirectory 'test/X', containing:
- a script 'TestX.sh' to run the test
- an output directory 'output' containing output of the test with prefix 'GoldStandard_'
Calling 'TextX.sh' starts the automated test and produces output in 'test/X/output/', which can be (manually) compared to the previously generated 'GoldStandard_'-output.
A first step towards fully automated tests of R-scripts may be the unraveling of R-functions and the main R-code (say main.R) that calls the functions. Functions may be put in a separate file (say main.functions.R), accompanied with their respective tests in another file (say test.main.functions.R). The package 'RUnit' (next section) provides some basic tools to support function tests. The default naming convention for test functions in the RUnit package is 'test.function' as is standard in JUnit.
The use of the package 'argparse' enables us to define the data type (int, character, double, ...) of the command line arguments, so that each call of the script is automatically validated before it starts executing.
The RUnit framework is completely implemented within R and does not rely on external tools or other language systems. The basic principle is that every function or method is accompanied with a test case that queries many calling situations including incorrect invocations.
Example of simple tests we could start with:
library(RUnit)
square(x) = function(x) x^2
test.square = function() {
checkEquals(square(5), 25)
checkException(square("circle"))
}
See also: CRAN / RUnit