Giter VIP home page Giter VIP logo

testex's Introduction

testex test examples

CRAN R CMD check coverage

Add tests and assertions in-line in examples

Quick Start

Set up your package to use testex using

testex::use_testex()

and then start adding tests!

#' Hello, World!
#' 
#' @examples
#' hello("World")
#' @test "Hello, World!"
#'
#' hello("darkness my old friend")
#' @test grepl("darkness", .)
#' 
#' @export
hello <- function(who) {
  paste0("Hello, ", who, "!")
}

If you were already using testthat, you'll immediately see a new test context for testing your examples. And if you aren't using testthat, then you'll find that your tests are being run with your examples when you run R CMD check

roxygen2 tags

@test

will check that the result of the last example is identical to your test. You can use the example output in a function using a ..

#' @examples
#' sum(1:10)
#' @test 55
#' @test is.numeric(.)

@testthat

is similar, but has the added benefit of automatically inserting a . into testthat::expect_* functions.

#' @examples
#' sum(1:10)
#' @testthat expect_equal(55)
#' @testthat expect_vector(numeric())

Prior Art

  • roxytest A slightly different approach. Allows tests to be written in-line, but generates test files used directly by a testing framework.

testex's People

Contributors

dgkf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

testex's Issues

`roxygen2` requests

Just keeping a few notes on changes that would help support this package in roxygen2:

  • Duplicate @examples are joined with extra empty lines. Especially when testex tags are functionally special @examples tags, this introduces a lot of unintentional whitespace.
  • @examplesIf changes the output value of the code, meaning that .Last.value is not what might be otherwise expected by tests.
    withAutoprint(1 + 2)
    .Last.value
    # == list(value = 3, visible = TRUE)
    
    instead would be preferred if the code that executes a @examplesIf statement returns only the value:
    if (<condition>) (if (getRversion() >= "3.4") (function(x) \{ x <- as.list(substitute(x))[-1]; invisible(withAutoprint(x, evaluated = TRUE)$value) \}) else force)(\{ # examplesIf}
    1 + 2
    \dontshow{\}) # examplesIf}
    

`testex::use_testex_as_testthat()` automatic re-documentation?

Should documentation automatically be generated when testex is run as testthat?

Without re-documenting, there's a risk that tests have been changed, but wouldn't yet be reflected. This is expected if someone is mindful about how testex works, but this is probably not a safe assumption and should probably "just work" in most cases.

Other user-facing behavior ideas:

  • Should it rebuild if roxygen2 version is different?
  • Should it be configurable? Parameter? DESCRIPTION option?
  • Should it overwrite package docs, or just build in a temp folder? Maybe emit a warning if documentation is out of date?

Handle running tests for documentation generated with older `testex`

Similar to roxygen2, this package will likely require some special attention for lifecycle management.

Since Rd files are generated with one version of testex, which might not necessarily be the one used when the tests are executed, there should be some graceful handling of this version mismatch.

For consistency, it should probably follow roxygen2's model of storing some data in the DESCRIPTION to record which version was used when the Rd files were generated. Then, this version can be checked against the loaded version when the tests are executed, failing gracefully when there is a version mismatch.

In such a scenario, it could silently generate new documentation in a temp folder, which it then uses for tests or just prompt the developer to re-document with the newer version.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.