File: describe.Rd

package info (click to toggle)
r-cran-testthat 3.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,996 kB
  • sloc: cpp: 9,265; ansic: 37; sh: 15; makefile: 5
file content (66 lines) | stat: -rw-r--r-- 2,061 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/describe.R
\name{describe}
\alias{describe}
\title{describe: a BDD testing language}
\usage{
describe(description, code)
}
\arguments{
\item{description}{description of the feature}

\item{code}{test code containing the specs}
}
\description{
A simple BDD DSL for writing tests. The language is similiar to RSpec for
Ruby or Mocha for JavaScript. BDD tests read like sentences and it should
thus be easier to understand what the specification of a function/component
is.
}
\details{
Tests using the \code{describe} syntax not only verify the tested code, but
also document its intended behaviour. Each \code{describe} block specifies a
larger component or function and contains a set of specifications. A
specification is defined by an \code{it} block. Each \code{it} block
functions as a test and is evaluated in its own environment. You
can also have nested \code{describe} blocks.

This test syntax helps to test the intended behaviour of your code. For
example: you want to write a new function for your package. Try to describe
the specification first using \code{describe}, before your write any code.
After that, you start to implement the tests for each specification (i.e.
the \code{it} block).

Use \code{describe} to verify that you implement the right things and use
\code{\link[=test_that]{test_that()}} to ensure you do the things right.
}
\examples{
describe("matrix()", {
  it("can be multiplied by a scalar", {
    m1 <- matrix(1:4, 2, 2)
    m2 <- m1 * 2
    expect_equal(matrix(1:4 * 2, 2, 2), m2)
  })
  it("can have not yet tested specs")
})

# Nested specs:
## code
addition <- function(a, b) a + b
division <- function(a, b) a / b

## specs
describe("math library", {
  describe("addition()", {
    it("can add two numbers", {
      expect_equal(1 + 1, addition(1, 1))
    })
  })
  describe("division()", {
    it("can divide two numbers", {
      expect_equal(10 / 2, division(10, 2))
    })
    it("can handle division by 0") #not yet implemented
  })
})
}