RTest: pretty testing of R packages

XML is not just easier to read then pure R-Code, it comes with a feature, that is called XSD; “XML schema definition”.

Each XML test case we create can immediately be checked against a schema designed by the developer.

It can also be checked against our very own Rtest.

xsd.

This means the tester can double check the created test cases before even executing them.

This saves us a lot of time and gives a fixed structure to all test cases.

The reporting was implemented in HTML.

This is due to the many features HTML comes with for reporting.

It allows coloring of test results, linking to test cases and including images.

The main difference for the reporting in HTML between RTest and testthat is that RTest reports every test that shall be executed, not only the failed ones.

The test report will also include the value created by the function call and the one given as a reference.

The reader can see if the comparison really went right.

By this the test report contains way more information than the testthat console log.

An example of a test implementation with RTestPlease note the whole example is stored in a github gist.

Please star the gist if you like this example.

Given a function that sums up two columns:my_function <- function(data = data.

frame(x=c(1,2),y=c(1,2))){stopifnot(dim(data)[2]==2)data[,"sum"] <- apply(data,1,function(x){sum(x)})return(data)}2.

We want to have one successful and one non successful test.

Both will have three parts in the XML file:<params><reference><testspec>params accounts for input parametersreference for the output data.

frametestspec for whether the test shall run silently and what the tolerance isFor the successful test our test would look like this:You can immediately see one special feature of RTest.

It allows to use data sets for multiple tests, we store those data sets in the input-data tag.

This saves space in the file.

The dataset test01 will be used here.

Moreover a test description can be given for each test.

For each data.

frame stored in XML the types of the columns can be given in col-defs .

Here those are all numeric.

Theinput-datais now given here:It’s a data frame with the x column just carrying 1 and the y column just carrying 2.

The test shall create a data.

frame with the sum column being 3 in each row.

We can easily let the test fail by changing the reference tag and instead of having just 3 in the sum column we can add a 3.

5 to let the test fail.

The whole test case can be found inside the github gist with 90 rows.

3.

The execution of the test case is just one line of code.

You shall have your working directory in the directory with the XML file and my_function shall be defined in the global environment.

RTest.

execute(getwd(),"RTest_medium.

xml")4.

The test report now contains one successful and one failed test.

Both will be visualized:General test outcome in RTest test reportadditional information is given on all tests.

For the test that failed we caused it by setting the sum to be 3.

5 instead of 3.

It’s reported at the end of the table:example of a failed data frame comparison in RTestMoreover the Report contains information on the environment where the test ran:System information for an RTest test reportThat’s it.

Now you can test any package with RTest.

Further readingRTest github repositoryRTest documentation websiteWhy do we need human readable test for a programming language?Author’s LinkedIn page.. More details

Leave a Reply