7

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.

Community
  • 1
  • 1
Harold
  • 293
  • 3
  • 10
  • I suggest you narrow down your question into a programming one. Note also that these kind of questions have a high chance of being closed as "looking for a tool" or "opinion based". – Roman Luštrik Oct 12 '14 at 08:51
  • 1
    If you want the tests to be run by `R CMD check`, you might consider adding a `configure` or `cleanup` script that invokes some other script running those test files. See http://cran.r-project.org/doc/manuals/r-release/R-exts.html#Configure-and-cleanup for more details. – Kevin Ushey Oct 12 '14 at 20:41
  • @KevinUshey, thanks! This will definitely work! I was hoping there was already some canonical way to do this without writing additional scripts, but doesn't seem that way. – Harold Oct 12 '14 at 21:35
  • 1
    I agree -- but unfortunately, the built-in facilities for `R CMD check` only accommodate unit testing of `R` code. I think using a `configure` / `cleanup` script is still the best way to go, though. (Or a custom makefile if you are really adventurous...) – Kevin Ushey Oct 13 '14 at 04:47

1 Answers1

5

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.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Thanks for the response, Dirk. I've updated my question -- please let me know if it's still not clear. – Harold Oct 12 '14 at 16:30
  • Rcpp has a pretty well-defined scope. What you want here seems to be outside of that scope, at least to my understanding. Rcpp may still help you as I outlined, but you in essence have a completely different problem ("how do I ship and test a C++ library") which ex ante has nothing to with R. – Dirk Eddelbuettel Oct 12 '14 at 18:47