Automated testing of packages created using rstantools


I am using testthat for unit tests of a package interfacing with rstan and covr to measure test coverage. It is very hard to achieve > 95% coverage because the automatically generated C++ file src/stanExports_lgp.h has

} catch (const std::exception& e) 
    stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());	
    // Next line prevents compiler griping about no return
    throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");	

in so many places, and those lines will never be reached when calling sampling or optimizing for the
stan model. Is there any way to improve coverage?

My current practice for testing Stan code is to run rstan::sampling for just a few iterations, so that it doesn’t take long, and to do that inside suppressWarnings because I know that there will be a warning about low Rhat etc. If someone knows a better way then I am happy to hear. Also I test the functions defined in functions block separately by calling them from R.

1 Like

It should be possible to specify the functions to be excluded by covr, although the documentation only refers to R functions, so that may not work over C/C++ headers.

Another option is to add some exclusion comments to the src/stanExports.lgp.h (I assume this is under your control), but again I don’t have direct experience whether it would work.