README.md

testdown

Lifecycle:
maturing R-CMD-check

The goal of {testdown} is to generate a bookdown report of {testthat} results

Installation

You can install the dev version of {testdown}

# Enable universe(s) by thinkr-open
options(repos = c(
    thinkropen = 'https://thinkr-open.r-universe.dev',
    CRAN = 'https://cloud.r-project.org'))

# Install some packages
install.packages('testdown')
remotes::install_github("ThinkR-open/testdown")

About

This package has two exported functions:

test_down()

This function turns the {testthat} results into a {bookdown} report. It takes:

test_down_example()

This function will compile the report for an example package contained inside {testdown}.

Custom {testdown} roxygen tags

You can add a description to your tests using the roxygen tag @description. Note that this test must be on the line just before the expectation.

Here is an example:

test_that("hello_world() works", {

  #' @description Testing that hello_world("colin") returns "Hello world, my name is colin!"
  expect_equal(hello_world("colin"), "Hello world, my name is colin!")

  #' @description Testing that hello_world("colin") returns a character vector
  expect_is(hello_world("colin"), "character")

  #' @description Testing that hello_world("colin") has World in it
  expect_match(hello_world("colin"), "World")

  #' @description Testing that hello_world("colin") has Hello in it
  expect_match(hello_world("colin"), "Hello")
})

How to read {testdown} reports

Glossary

Scopes of tests

There are three scopes when it comes to tests:

To sum up, Test files contains one or more test(s), which contain one or more expectation(s).

Expectations status

First page

The index offers a summary of:

Global results

This page offers a summary of the results, grouped by test.

Details of the table:

Test-files.R

Each test file has its own chapter, detailing the results of the expectations.

The summary at the top gives an overview of the results for the given test file.

Then, details are included about the tested file:

Aggregated

Aggregation of expectations that returned a failure/error, warning, or were skipped.

Writing custom expectation

For reliable results, all expectations should start by expect, notably if you need to count the skipped tests. In other words, the skipped expectations count relies on counting all the functions starting with expect, so naming custom expectation differently will prevent this count from being correct.

Known limitations

{testdown} report relies on {testthat} and the results are based on these outputs.

Here are known example of results that will be discarded by {testthat} and hence will make {testdown} behave in a weird way.

test_that("Files exist", {
  with_dir(
    "new/dir",
    {
      #' @description Checking that style.css is created
      expect_file_exists("tests/testdown/style.css")
      #' @description Checking that _bookdown.yml is created
      expect_file_exists("tests/testdown/_bookdown.yml")
      #' @description Checking that _output.yml is created
      expect_file_exists("tests/testdown/_output.yml")
    }
  )
})

As testthat doesn’t count the expectations from with_dir, this will make the {testdown} result weird. Always add the expectations at the top level of your test_that.

Same goes with for loops:

for (i in names(df)){
    #' @description Checking the names of the output are correct
    expect_true(
      i %in% c(
        "context",
        "test",
        "expectation",
        "description",
        "location",
        "test_time",
        "result",
        "file",
        "message"
      )
    )
  }

If ever you use this format, {testthat} won’t catch the tests, so they won’t be reported.

As testdown render the text straight in html, if your expecatation contains html, it will break the rendering. For example, the following test will break the rendering:

expect_match(
  tag, 
  "<h2>this</h2>"
)

Sponsor

The development of this package has been sponsored by:

CoC

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.



ThinkR-open/testdown documentation built on April 17, 2023, 3:21 a.m.