Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: throw errors if a function is not tested?

I would like to provide an additional reminder / incentive to my colleagues to write tests for new functions that they contribute to a package I have developed.

I am not sure if this is even a good idea, or if there is a better approach, but I would like to know if there is a way to use the automated testing of files in /tests and /inst/tests to encourage developers to write tests for new functions.

For example, is there a test that will throw an error when the package is checked, built, or tested if there are any functions in a package without an associated test?

I recognize that there are many potential problems with this. It would be difficult to determine if the test is any good, and it might be a waste of time to test a trivial function. But it would still serve as a reminder that a test should be written, and perhaps it could enforce one or two very basic rules.

Is there a test that I can write that will prevent the package from compiling if there is a function without a test? I am using the testthat package in R.

It doesn't need to be terribly robust, but the ability to exclude functions by name would be useful.

like image 647
David LeBauer Avatar asked Apr 14 '26 01:04

David LeBauer


1 Answers

It sounds like you're looking for "code coverage" functionality. The first step here would be to figure out which code is covered by tests and which isn't. Check out section 2.2 of the RUnit Vignette for information on how to do that.

You may end up having to modify the inspect function in order to throw alerts or handle custom events like that, but I think they have the bulk of what you're trying to do already implemented.

like image 191
Jeff Allen Avatar answered Apr 15 '26 15:04

Jeff Allen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!