Mocha-style BDD framework for testthat.
While testthat includes a basic describe
/it
framework, it does not
work very well with the provided reporters and does not support the
setup blocks (before_each
). This package tries to solve these
problems by adding setup blocks and providing a new MochaReporter
which prints results in the familiar mocha-like style.
For now, the package is not uploaded to CRAN so you have to install it
manually or using renv
or devtools
:
devtools::install_github("ydistri/testthatBdd")
## or
renv::install("ydistri/testthatBdd")
In general the usage is the same as mochajs so feel free to refer to their documentation. Here we recount briefly the main features.
Create a test file as usual for testthat
, but instead of using
test_that
, use a combination of describe
and it
blocks:
library(testthat)
library(testthatBdd)
test_that("addition works", {
expect_equal(1 + 1, 2)
expect_equal(10 + 10, 20)
})
## becomes
describe("addition", {
it("works with small numbers", {
expect_equal(1 + 1, 2)
})
it("works with big numbers", {
expect_equal(10 + 10, 2)
})
})
The describe blocks can be nested to further specify the parts of the program.
You can write a common setup block for each it
expectation using a
before_each
. They can be written inside a describe
section on any
level and run in order from the most general to the most specific
before the it
code is executed.
library(testthat)
library(testthatBdd)
describe("addition", {
before_each({
a <- 1
})
describe("with small numbers", {
it("works", {
expect_equal(a + a, 2)
})
})
describe("with big numbers", {
before_each({
## make the number bigger!
a <- a * 10
})
it("works", {
expect_equal(a + a, 20)
})
})
})
To skip an individual test, change the it
to xit
, it will then be
makred as skipped. To skip an entire suite, change describe
to
xdescribe
and all the nested describe
and it
sections
(recursively) will be skipped. You can also use the standard skip_*
functions from testthat
as usual.
describe("addition", {
it("works with small numbers", {
expect_equal(1 + 1, 2)
})
xit("works with big numbers", {
expect_equal(10 + 10, 2)
})
it("works with enormous numbers", {
skip("Platform does not support enormous numbers")
expect_equal(100000000000 + 100000000000, 2)
})
})
$ Rscript -e 'devtools::test(reporter = MochaReporter$new())'
addition
✓ works with small numbers
? works with big numbers
? works with enormous numbers
3 tests, 0 failures, 2 skipped, 0.27 seconds
To use the mocha-style reporter, set the reporter to MochaReporter
:
$ Rscript -e 'devtools::test(reporter = MochaReporter$new())'
addition
with small numbers
✓ works
with big numbers
✓ works
2 tests, 0 failures, 0 skipped, 0.1 seconds
The reporter might or might not work nicely with the standard
test_that
style tests, so use at your own risk.
Currently, only the before_each
setup section is implemented.
before
, after
and after_each
are not implemented.