I am trying to test a individual functions in a R script which contains a function call as the last statement of the script i.e. Here is the R code:
mul.R
#!/usr/bin/env Rscript
mul <- function(n, m) {
prod <- n * m
return (prod)
}
mul(4,5)
Here is the test script:
test_simpleProgram.R
#!/usr/bin/env Rscript
library('RUnit')
source("./../simpleProgram.R")
test.mul <- function() {
checkEqualsNumeric(mul(n= 2,m= 3), 4)
}
I execute the test script using the following command:
runTestFile(absFileName= file.path("test_simpleProgram.R"))
Now, when I source the R script I want to test i.e. mul.R, since the last statement already makes a call to the mul function, the entire script is executed and then the test script runs the function again. This makes the 1st execution(due to source(..)) not needed for testing purpose.
Is there a way to test such scripts, without running the script when sourcing it in the test script?
mulfunction and maybe eventest.mul. Then both your script and test script will source that file.