Assuming I want to write unit tests for the following function using the package testthat
:
make_matrix = function(vec) {
matrix(vec, nrow=2)
}
The expected function is as follows:
make_matrix(seq(1:4))
[,1] [,2]
[1,] 1 3
[2,] 2 4
Following suggestions on SO I will store the input for the test functions in inst/testdata
Where should I put data for automated tests with testthat?
#input = system.file("testdata/input.rds, package "my_package")
input = seq(1:4)
# expected_output_for_seq_1_4 = readRDS()/system.file()?
expected_output_for_seq_1_4 = matrix(c(1,2,3,4), nrow=2)
test_that("basic_test", {
expect_equal(make_matrix(input), expected_output_for_seq_1_4)
})
But where and how shall I store the expected outputs? Also in inst/testdata
? Given a more complex function there could be many expected outputs for a single input dependent on the functions' parameters