Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing Rcpp code in a package

I have some C++ code in a package that I want to unit test and I don't want to export to R. I've seen:

Unit tests for code in the /src folder of an R package?

and the code I want to test falls into the second category:

Or you consider your C code to be more standalone, in which case you could use one of a bazillion unit testing frameworks for C.

If one wants to test from C++ (not in using a R framework) what is the best way to setup the test suite? Do you put your C++ tests in tests/ and then have a C++ program with a main somewhere, or do you write a driver function to execute all the C++ tests and then call it from R?

I imagine it is probably a good idea to have the tests be automated by R CMD check as well, which I'm not clear on how to automate using the R package framework.

like image 250
Harold Avatar asked Oct 12 '14 05:10

Harold


People also ask

Can unit testing of code be automated?

Unit testing is a software development and testing approach in which the smallest testable parts of an application, called units, are individually and independently tested to see if they are operating properly. Unit testing can be done manually but is usually automated.

Should you write unit tests before code?

It often makes sense to write the test first and then write as much code as needed to allow the test to pass. Doing this moves towards a practice known as Test-Driven Development (TDD). Bluefruit uses a lot of TDD because it helps us to build the right product without waste and redundancies.

What does unit testing do for code quality?

Unit tests in isolation One of the benefits of unit tests is that they isolate a function, class or method and only test that piece of code. Higher quality individual components create overall system resiliency. Thus, the result is reliable code. Unit tests also change the nature of the debugging process.


1 Answers

I do not follow:

  • Either it is Rcpp code, but you do not want to expose it to R: The moment you wan to expose it to the (R) unit tests (in the packaging framework) you do though.
  • Or it is just a question of picking one of (a great many) C++ unit testing framework like the Boost Test library, or the Google C++ Testing library, or ... --- none of which has been ported to R "as such"

Can you clarify your question a little more?

Maybe you just want to express your C++ code in a way that lets Rcpp call it from only the unit tests so that you get results you can then test via one of popular R unit testing frameworks. In which case the answer may be very similar to what any one of the (currently) 276 Rcpp-using packages on CRAN do as many do in fact use unit tests.

like image 163
Dirk Eddelbuettel Avatar answered Sep 29 '22 03:09

Dirk Eddelbuettel