Skip to content

[Experimental]

Runs the automated tests, as provided in the -tests chunk, of an exercise. This function is called internally when testing is enabled in a tutorial. To enable automated testing while rendering a tutorial — that is, when calling rmarkdown::render(), rmarkdown::run(), or clicking the Run Document button in the RStudio IDE — set the LEARNR_TEST environment variable to TRUE, e.g.

Sys.setenv(LEARNR_TEST = "TRUE")

This can either be set in the environment where the tutorial is being rendered, or more directly you may add the line above at the start of your setup chunk.

Usage

test_that_exercise(exercise, test_solutions = NULL)

Arguments

exercise

An exercise object as returned by mock_exercise().

test_solutions

[logical(1)]
If TRUE, test_that_exercise() will automatically test that the exercise solution, if present, returns valid feedback that is marked as correct. It does not automatically check the message or other properties of the feedback, so you may still wish to include tests for the correct solution in your -tests chunk.

You can also set the environment variable LEARNR_TEST_SOLUTIONS to FALSE to disable automated solution testing.

Value

Nothing.

See also

Other exercise testing functions: expect_feedback()

Examples

# Here's an example exercise where students would be asked
# to subset `letters` to its first four elements. There's
# some very basic checking code (but normally you'd want to
# use {gradethis}).
#
# This example was also used for `?expect_feedback()`, but
# we've now added a series of tests to the `-tests` chunk
# of our example exercise, which is mocked using the `tests`
# argument of `mock_exercise()`.

ex <- mock_exercise(
  user_code = "",
  solution_code = "letters[1:4]",
  check = '
  is_correct <- identical(last_value, solution)
  list(
    correct = is_correct,
    message = if (is_correct) {
      "Great!"
    } else if (length(last_value) != 4) {
      "I expected a vector with four items."
    } else {
      "Try again!"
    }
  )
  ',
  tests = '
  test_that("The solution is marked as correct", {
    expect_feedback(letters[1:4], correct = TRUE, message = "Great!")
  })

  test_that("Vectors of the wrong size are marked as incorrect", {
    expect_feedback(letters[1:3], correct = FALSE, message = "four items")
    expect_feedback(letters[1:6], correct = FALSE, message = "four items")
    expect_feedback(letters[1], correct = FALSE, message = "four items")
  })

  test_that("Other incorrect answers are marked as incorrect", {
    expect_feedback(letters[4:1], correct = FALSE, message = "Try again!")
  })
  ',
  exercise.checker = "function(last_value, check_code, solution_code, ...) {
    solution <- eval(parse(text = solution_code))
    eval(parse(text = check_code))
  }"
)

if (requireNamespace("testthat", quietly = TRUE)) {
  library(testthat)
  test_that_exercise(ex)
}
#> Test passed 🥳
#> Test passed 😀
#> Test passed 🥇
#> Test passed 🎊