Package 'hySpc.testthat'

Title: 'testthat' Unit Test Enhancements
Description: Enhance package 'testthat' by allowing tests to be attached to the function/object they test. This allows to keep functional and unit test code together.
Authors: Claudia Beleites [aut, cre], Erick Oduniyi [aut]
Maintainer: Claudia Beleites <[email protected]>
License: MIT + file LICENSE
Version: 0.2.1
Built: 2025-02-14 05:11:54 UTC
Source: https://github.com/cran/hySpc.testthat

Help Index


Get test that is attached to object as "test" attribute

Description

The returned function can be executed. If that is done within a testthat::Reporter, the results will be reported accordingly. Without reporter, the test runs silently if successful and stops if it fails.

Usage

get_test(object)

Arguments

object

to which the test is attached

Value

the test (function)

Examples

f <- function(x) x^2

test(f) <- function() {
   context("f")

   test_that("correct result for complex number", {
     expect_equal(f(1i), -1 + 0i)
   })
}

get_test(f)

library(testthat)
## execute the test
get_test(f)()

with_reporter("summary", get_test(f)())

Run test attached to function

Description

Execute test attached to a function with testthat::Reporter.

Usage

test_fun(object, reporter = "minimal")

Arguments

object

to which the test is attached

reporter

testthat::Reporter to use

Value

the test (function)

Examples

f <- function(x) x^2

test(f) <- function() {
   context("f")

   test_that("correct result for complex number", {
     expect_equal(f(1i), -1 + 0i)
   })
}

test_fun(f)

Attach unit tests to objects

Description

This function attaches unit tests in value to an object (typically a function) as an attribute "test".

Usage

test(f) <- value

Arguments

f

the function (object) to which the tests are to be attached

value

the test code, a function with no parameters

Value

f with the test attached as attribute "test"

Examples

f <- function(x) x^2

test(f) <- function() {
   context("f")

   test_that("correct result for complex number", {
     expect_equal(f(1i), -1 + 0i)
   })
}

Run unit tests

Description

If testthat::testthat-package is available, run the unit tests and display the results.

Usage

unittest(ns, standalone = TRUE, reporter = "progress")

Arguments

ns

namespace (package) to test

standalone

if TRUE, appropriate testthat::Reporters are used, if FALSE the exectution assumes reporters are started already.

reporter

testthat::Reporter to be used.

Value

Invisibly returns a data.frame with the test results

Author(s)

Claudia Beleites

Examples

unittest("hySpc.testthat")