Skip to content

Commit

Permalink
Lowercase skip context in test_dir and use capture.output instead of …
Browse files Browse the repository at this point in the history
…sink
  • Loading branch information
Neal Richardson committed Sep 28, 2015
2 parents 426df15 + 24d76a1 commit 502c900
Show file tree
Hide file tree
Showing 23 changed files with 84 additions and 56 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
^cran-comments\.md$
^appveyor\.yml$
^revdep$
^.*\.o$
32 changes: 8 additions & 24 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
# Sample .travis.yml for R projects from https://github.com/craigcitro/r-travis

language: c
language: r
sudo: required
warnings_are_errors: true

env:
- global:
- WARNINGS_ARE_ERRORS=1


before_install:
- curl -OL http://raw.github.com/craigcitro/r-travis/master/scripts/travis-tool.sh
- chmod 755 ./travis-tool.sh
- ./travis-tool.sh bootstrap
global:
- CRAN: http://cran.rstudio.com

install:
- ./travis-tool.sh install_deps
- ./travis-tool.sh github_package jimhester/covr

script: ./travis-tool.sh run_tests

on_failure:
- ./travis-tool.sh dump_logs
r_packages:
- covr

after_success:
- Rscript -e 'library(covr);coveralls()'

notifications:
email:
on_success: change
on_failure: change
- Rscript -e 'covr::codecov()'
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export(shows_message)
export(skip)
export(skip_if_not_installed)
export(skip_on_cran)
export(skip_on_os)
export(skip_on_travis)
export(source_dir)
export(source_test_helpers)
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# testthat 0.10.0.9000

* `comparison()` constructure now checks its arguments are the correct type and
length. This bugs a bug where tests failed with an error like "values must be
length 1, but FUN(X[[1]]) result is length 2" (#279).

* Added `skip_on_os()`, to skip tests on specified operating systems
(@kevinushey).

# testthat 0.10.0

* Failure locations are now formated as R error locations.
Expand Down
4 changes: 4 additions & 0 deletions R/compare.r
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ compare <- function(x, y, ...) {
}

comparison <- function(equal = TRUE, message = "Equal") {
stopifnot(is.logical(equal), length(equal) == 1)
stopifnot(is.character(message), length(message) == 1)

structure(
list(
equal = equal,
Expand Down Expand Up @@ -136,6 +139,7 @@ compare.numeric <- function(x, y, max_diffs = 10, ...) {
if (isTRUE(equal)) return(comparison())

# If they're not the same type or length, fallback to default method
equal <- paste0(equal, collapse = "\n")
if (!is.integer(x) && !is.numeric(y)) return(comparison(FALSE, equal))
if (length(x) != length(y)) return(comparison(FALSE, equal))

Expand Down
2 changes: 1 addition & 1 deletion R/expectations-equality.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#' scale = expectedValue)
#' }
#'
#' # expect_equalivalent ignores attributes
#' # expect_equivalent ignores attributes
#' a <- b <- 1:3
#' names(b) <- letters[1:3]
#' expect_equivalent(a, b)
Expand Down
3 changes: 3 additions & 0 deletions R/mock.r
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#' On exit (regular or error), all functions are restored to their previous state.
#' This is somewhat abusive of R's internals, and is still experimental, so use with care.
#'
#' Primitives (such as \code{\link[base]{interactive}}) cannot be mocked, but this can be
#' worked around easily by defining a wrapper function with the same name.
#'
#' @param ... named parameters redefine mocked functions, unnamed parameters
#' will be evaluated after mocking the functions
#' @param .env the environment in which to patch the functions,
Expand Down
2 changes: 1 addition & 1 deletion R/reporter-stop.r
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ NULL
#' Test reporter: stop on error.
#'
#' The default reporter, executed when \code{expect_that} is run
#' interactively, or when the test files are executed by R CMD check. It
#' interactively. It
#' responds by \link{stop}()ing on failures and doing nothing otherwise. This
#' will ensure that a failing test will raise an error.
#'
Expand Down
2 changes: 1 addition & 1 deletion R/test-example.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Test package examples
#'
#' These helper functions make it easier to test the examples in a pakcage.
#' These helper functions make it easier to test the examples in a package.
#' Each example counts as one test, and it succeeds if the code runs without
#' an error.
#'
Expand Down
2 changes: 1 addition & 1 deletion R/test-results.r
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ sumarize_one_test_results <- function(test) {
context <- if (length(test$context) > 0) test$context else ''

res <- data.frame(file = test$file, context = context, test = test$test,
nb = nb_tests, failed = nb_failed, skipped = nb_skipped, error = error,
nb = nb_tests, failed = nb_failed, skipped = as.logical(nb_skipped), error = error,
user = test$user, system = test$system, real = test$real,
stringsAsFactors = FALSE)

Expand Down
10 changes: 5 additions & 5 deletions R/test-that.r
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' A test encapsulates a series of expectations about small, self-contained
#' set of functionality. Each test is contained in a \link{context} and
#' contains multiple expectation generated by \code{\link{expect_that}}.
#' contains multiple expectation generated by \code{\link{equivalence}}.
#'
#' Tests are evaluated in their own environments, and should not affect
#' global state.
Expand All @@ -16,14 +16,14 @@
#' @export
#' @examples
#' test_that("trigonometric functions match identities", {
#' expect_that(sin(pi / 4), equals(1 / sqrt(2)))
#' expect_that(cos(pi / 4), equals(1 / sqrt(2)))
#' expect_that(tan(pi / 4), equals(1))
#' expect_equal(sin(pi / 4), 1 / sqrt(2))
#' expect_equal(cos(pi / 4), 1 / sqrt(2))
#' expect_equal(tan(pi / 4), 1)
#' })
#' # Failing test:
#' \dontrun{
#' test_that("trigonometric functions match identities", {
#' expect_that(sin(pi / 4), equals(1))
#' expect_equal(sin(pi / 4), 1)
#' })
#' }
test_that <- function(desc, code) {
Expand Down
25 changes: 22 additions & 3 deletions R/utils.r
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,42 @@ skip <- function(message) {
#' @param pkg Name of package to check for
#' @rdname skip
skip_if_not_installed <- function(pkg) {
if (requireNamespace(pkg, quietly = TRUE)) return()
if (requireNamespace(pkg, quietly = TRUE)) return(invisible(TRUE))

skip(paste0(pkg, " not installed"))
}

#' @export
#' @rdname skip
skip_on_cran <- function() {
if (identical(Sys.getenv("NOT_CRAN"), "true")) return()
if (identical(Sys.getenv("NOT_CRAN"), "true")) return(invisible(TRUE))

skip("On CRAN")
}

#' @export
#' @param os Character vector of system names. Supported values are
#' \code{"windows"}, \code{"mac"}, \code{"linux"} and \code{"solaris"}.
#' @rdname skip
skip_on_os <- function(os) {
os <- match.arg(os, c("windows", "mac", "linux", "solaris"),
several.ok = TRUE)
sysname <- tolower(Sys.info()[["sysname"]])

switch(sysname,
windows = if ("windows" %in% os) skip("On windows"),
darwin = if ("mac" %in% os) skip("On Mac"),
linux = if ("linux" %in% os) skip("On Linux"),
sunos = if ("solaris" %in% os) skip("On Solaris")
)

invisible(TRUE)
}

#' @export
#' @rdname skip
skip_on_travis <- function() {
if (!identical(Sys.getenv("TRAVIS"), "true")) return()
if (!identical(Sys.getenv("TRAVIS"), "true")) return(invisible(TRUE))

skip("On Travis")
}
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# testthat

[![Travis-CI Build Status](https://travis-ci.org/hadley/testthat.png?branch=master)](https://travis-ci.org/hadley/testthat) [![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/hadley/testthat?branch=master)](https://ci.appveyor.com/project/hadley/testthat)
[![Coverage Status](https://img.shields.io/coveralls/hadley/testthat.svg)](https://coveralls.io/r/hadley/testthat?branch=master)
[![Travis-CI Build Status](https://travis-ci.org/hadley/testthat.svg?branch=master)](https://travis-ci.org/hadley/testthat)
[![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/hadley/testthat?branch=master&svg=true)](https://ci.appveyor.com/project/hadley/testthat)
[![Coverage Status](https://img.shields.io/codecov/c/github/hadley/testthat/master.svg)](https://codecov.io/github/hadley/testthat?branch=master)
[![CRAN RStudio mirror downloads](http://cranlogs.r-pkg.org/badges/testthat)](http://cran.r-project.org/web/packages/testthat/index.html)
[![CRAN version](http://www.r-pkg.org/badges/version/testthat)](http://cran.r-project.org/web/packages/testthat/index.html)

Testing your code is normally painful and boring. `testthat` tries to make testing as fun as possible, so that you get a visceral satisfaction from writing tests. Testing should be fun, not a drag, so you do it all the time. To make that happen, `testthat`:

Expand Down Expand Up @@ -30,7 +33,7 @@ library(yourpackage)
test_package("yourpackage")
```

Now, recommended practice is to put your tests in `tests/testthat`, and ensure `R CMD check` runs them by putting the following code in `tests/testthat.R`:
Now, recommended practice is to put your tests in `tests/testthat` in files starting with `test`, and ensure `R CMD check` runs them by putting the following code in `tests/testthat.R`:

```R
library(testthat)
Expand Down
2 changes: 1 addition & 1 deletion man/StopReporter-class.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
\description{
The default reporter, executed when \code{expect_that} is run
interactively, or when the test files are executed by R CMD check. It
interactively. It
responds by \link{stop}()ing on failures and doing nothing otherwise. This
will ensure that a failing test will raise an error.
}
Expand Down
2 changes: 1 addition & 1 deletion man/equivalence.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ expect_equal(object = 10.01, expected = expectedValue, tolerance = 0.002,
scale = expectedValue)
}

# expect_equalivalent ignores attributes
# expect_equivalent ignores attributes
a <- b <- 1:3
names(b) <- letters[1:3]
expect_equivalent(a, b)
Expand Down
6 changes: 6 additions & 0 deletions man/skip.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
\alias{skip}
\alias{skip_if_not_installed}
\alias{skip_on_cran}
\alias{skip_on_os}
\alias{skip_on_travis}
\title{Skip a test.}
\usage{
Expand All @@ -13,12 +14,17 @@ skip_if_not_installed(pkg)

skip_on_cran()

skip_on_os(os)

skip_on_travis()
}
\arguments{
\item{message}{A message describing why the test was skipped.}

\item{pkg}{Name of package to check for}

\item{os}{Character vector of system names. Supported values are
\code{"windows"}, \code{"mac"}, \code{"linux"} and \code{"solaris"}.}
}
\description{
This function allows you to skip a test if it's not currently available.
Expand Down
2 changes: 1 addition & 1 deletion man/test_examples.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ For \code{test_example}, path to a single Rd file. Remember the working
directory for tests is \code{tests/testthat}.}
}
\description{
These helper functions make it easier to test the examples in a pakcage.
These helper functions make it easier to test the examples in a package.
Each example counts as one test, and it succeeds if the code runs without
an error.
}
Expand Down
10 changes: 5 additions & 5 deletions man/test_that.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ are often used as line prefixes.}
\description{
A test encapsulates a series of expectations about small, self-contained
set of functionality. Each test is contained in a \link{context} and
contains multiple expectation generated by \code{\link{expect_that}}.
contains multiple expectation generated by \code{\link{equivalence}}.
}
\details{
Tests are evaluated in their own environments, and should not affect
Expand All @@ -26,14 +26,14 @@ expectations are met, otherwise it raises an error.
}
\examples{
test_that("trigonometric functions match identities", {
expect_that(sin(pi / 4), equals(1 / sqrt(2)))
expect_that(cos(pi / 4), equals(1 / sqrt(2)))
expect_that(tan(pi / 4), equals(1))
expect_equal(sin(pi / 4), 1 / sqrt(2))
expect_equal(cos(pi / 4), 1 / sqrt(2))
expect_equal(tan(pi / 4), 1)
})
# Failing test:
\dontrun{
test_that("trigonometric functions match identities", {
expect_that(sin(pi / 4), equals(1))
expect_equal(sin(pi / 4), 1)
})
}
}
Expand Down
3 changes: 3 additions & 0 deletions man/with_mock.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ available when testing.
This works by using some C code to temporarily modify the mocked function \emph{in place}.
On exit (regular or error), all functions are restored to their previous state.
This is somewhat abusive of R's internals, and is still experimental, so use with care.
Primitives (such as \code{\link[base]{interactive}}) cannot be mocked, but this can be
worked around easily by defining a wrapper function with the same name.
}
\examples{
with_mock(
Expand Down
6 changes: 1 addition & 5 deletions tests/testthat/test-reporter-tap.r
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
context("TAP reporter")

test_that("TAP reporter handles context and pass/fail/skip", {
filename <- tempfile(fileext="tap")
sink(filename)
test_dir("test_dir", reporter = "tap")
sink()
tap.report <- readLines(filename)
tap.report <- capture.output(test_dir("test_dir", reporter = "tap"))
expect_identical(tap.report[1], "1..24")
expect_identical(tap.report[2], "# Context Bare ")
expect_true("ok 7 equality holds " %in% tap.report)
Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/test-test_dir.r
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test_that('test_dir()', {
"test-helper.r", "test-skip.r"),
context = c("Basic", "Basic", "Basic", "Basic",
"Basic", "empty", "empty", "error", "error", "error", "error",
"error", "failures", "failures", "failures", "helper", "Skip"),
"error", "failures", "failures", "failures", "helper", "skip"),
test = c("logical tests act as expected",
"logical tests ignore attributes", "equality holds",
"can't access variables from other tests 2",
Expand All @@ -24,7 +24,8 @@ test_that('test_dir()', {
"Skips skip"),
nb = c(2L, 2L, 2L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 2L, 2L, 1L, 1L),
failed = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L),
skipped = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L),
skipped = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE),
error = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE,
TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE),
stringsAsFactors = FALSE)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-watcher.r
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test_that("compare state works correctly", {
})

test_that("watcher works correctly", {
skip_on_cran()
skip_on_cran() && skip_on_os("windows")

loc <- tempfile("watcher", tmpdir = "/tmp")
dir.create(loc)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test_dir/test-skip.r
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context("Skip")
context("skip")

test_that("Skips skip", {
skip("Skipping to avoid certain failure")
Expand Down

0 comments on commit 502c900

Please sign in to comment.