Question

I'm relatively new to R and am trying to develop an example package which has documentation, unit testing etc baked in to enable myself and others in the company to reference it for best practices.

With that in mind, I have an R function saved into the R folder of the package structure (created by RStudio, in a git branch)

#' Loads the typical example for a specific modelling problem
#' 
#' @keywords helper dataset logistic
#' 
#' @param purpose Provide the type of modelling you're looking to do
#' @return dataset A dataset should be added to the workspace
#' 
#' @examples
#' relevantTestDataset(purpose="logistic")
#' relevantTestDataset("logistic")
#' 
#' @export


relevantTestDataset<-function(purpose)
{

  if (purpose=="logistic") 
    {
    loadedPackage<-"caret"
    loadedData<-c("GermanCredit")
      }

  data(list=loadedData,package=loadedPackage)
}

I've created the following test (saved to a tests folder in line with the stringr structure)

context("Basic functionality")

test_that("a dataset is loaded up for each expected model type",{
  expect_that(relevantTestDataset("a"),throws_error())

})

I then run test() in dev mode and get

Loading Example
Testing Example
NULL

I have tried test_dir("/tests/") and that returns an empty console line then moves on.

I've tried a variety of expectations to see if that was the issue, but all return the same console output. I've verified that the function does work by loading it into my workspace and executing it.

I'm sure I'm doing something totally stupid and obvious here, put could somebody tell me precisely where I'm going wrong?

Thanks in advance, Steph

Was it helpful?

Solution

Your tests should be in ~inst/tests/.

For example:

context("Basic functionality")

test_that("a dataset is loaded up for each expected model type",{
  expect_that(relevantTestDataset("a"),throws_error())  
})

should be in a file ~inst/tests/test1.R.

Moreover there should be a file ~tests/run-all.R:

library(testthat) 
library(<yourpackage>) 
test_package("<yourpackage>")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top