rstudio::conf(2020L)
—Hadley Wickham
use_test()
) using expectationsuse_test()
) using expectationstest()
, test_file()
, Cmd/Ctrl + Shift + T
.use_test()
) using expectationstest()
, test_file()
, Cmd/Ctrl + Shift + T
.check()
)test/testthat/test-themes.R
test_that("theme works", { expect_true(ggplot2::is.theme(theme_mako())) expect_error(theme_mako(base_size = "14"))})
test/testthat/test-themes.R
test_that("theme works", { expect_true(ggplot2::is.theme(theme_mako())) expect_error(theme_mako(base_size = "14"))})
test/testthat/test-themes.R
test_that("theme works", { expect_true(ggplot2::is.theme(theme_mako())) expect_error(theme_mako(base_size = "14"))})
test/testthat/test-themes.R
test_that("theme works", { expect_true(ggplot2::is.theme(theme_mako())) expect_error(theme_mako(base_size = "14"))})
test()
test/testthat/test-themes.R
test_that("theme works", { expect_true(ggplot2::is.theme(theme_mako())) expect_error(theme_mako(base_size = "14"))})
test()
## Loading shinRa## Testing shinRa## ✓ | OK F W S | Context## ✓ | 2 | themes## ## ══ Results ════════════════════════## OK: 2## Failed: 0## Warnings: 0## Skipped: 0
test/testthat/test-themes.R
test_that("theme works", { expect_true(ggplot2::is.theme(theme_mako())) expect_error(theme_mako(base_size = "14")) expect_equal(theme_mako(), ggplot2::theme_dark())})
test()
Loading shinRa✓ | OK F W S | Contextx | 2 1 | themes────────────────────────────────────test-themes.R:4: failure: theme workstheme_mako() not equal to ggplot2::theme_dark().Component “line”: Component “size”: Mean relative difference: 0.2142857Component “rect”: Component “size”: Mean relative difference: 0.2142857Component “text”: Component “size”: Mean relative difference: 0.2142857Component “axis.title.x”: Component “margin”: Mean relative difference: 0.2142857Component “axis.title.x.top”: Component “margin”: Mean relative difference: 0.2142857Component “axis.title.y”: Component “margin”: Mean relative difference: 0.2142857Component “axis.title.y.right”: Component “margin”: Mean relative difference: 0.2142857Component “axis.text.x”: Component “margin”: Mean relative difference: 0.2142857Component “axis.text.x.top”: Component “margin”: Mean relative difference: 0.21
test/testthat/test-themes.R
test_that("theme works", { expect_true(ggplot2::is.theme(theme_mako())) expect_error(theme_mako(base_size = "14")) expect_equal(theme_mako(), ggplot2::theme_dark())})
expect_*()
)function | expectation |
---|---|
expect_equal(x, y) |
the same, more or less |
expect_identical(x, y) |
the exact same |
expect_message/warning/error(x, y) |
a message, warning, or error |
expect_true(x) |
TRUE |
expect_is(x, y) |
x is class y |
expect_*()
)function | expectation |
---|---|
expect_equal(x, y) |
the same, more or less |
expect_identical(x, y) |
the exact same |
expect_message/warning/error(x, y) |
a message, warning, or error |
expect_true(x) |
TRUE |
expect_is(x, y) |
x is class y |
MANY more. See https://r-pkgs.org/tests.html
use_test()
to create a new file. Call it "resident_connection"test_that()
) to "connection is returning valid data"test_that()
function, remove the default expectations. Replace them with this codetest_file("tests/testthat/test-resident_connection.R")
in the console.use_test("resident_connection")
test-resident_connection.R
test_that("connection is returning valid data", { # `resident_data` is a tibble, isn't empty, and has the right columns resident_data <- get_resident_data() expect_is(resident_data, c("tbl_df", "tbl", "data.frame")) expect_gt(nrow(resident_data), 0) expect_named(resident_data, c("sector", "residents")) # `resident_data_dt` is a data.table resident_data_dt <- get_resident_data(data_table = TRUE) expect_is(resident_data_dt, c("data.table", "data.frame"))})
test()
. Fix the broken tests.R/summarize_data.R
segment_reactor_output <- function(reactor_num, data_table = FALSE) { reactor_output <- hack_shinra_data(data_table = data_table) dplyr::filter(reactor_output, .data$reactor == reactor_num)}
use_r()
🤝 use_test()
skip_*()
)function |
---|
skip() |
skip_if() |
skip_on_cran() |
skip_on_travis() |
skip_on_os() |
skip_*()
)function |
---|
skip() |
skip_if() |
skip_on_cran() |
skip_on_travis() |
skip_on_os() |
MANY more. See ?testthat::skip
check()
or Cmd/Ctrl + Shift + E
check()
or Cmd/Ctrl + Shift + E
See all the details at https://r-pkgs.org/r-cmd-check.html
check()
or Cmd/Ctrl + Shift + E
check()
until you get a clean bill of health.segment_reactor_output()
is in R/summarize_data.R
use_package("dplyr")
#' Segment Shinra reactor data#'#' @param reactor_num The reactor number to segment by.#' @inheritParams hack_shinra_data#'#' @return a tibble or data.table filtered by `reactor_num`#' @export#'#' @examples#'#' segment_reactor_output(7)#'segment_reactor_output <- function(reactor_num, data_table = FALSE) { reactor_output <- hack_shinra_data(data_table = data_table) dplyr::filter(reactor_output, .data$reactor == reactor_num)}
result | meaning | fix required for CRAN? |
---|---|---|
Error | A severe problem. Always fix. | Yes |
Warning | A probable problem. Almost always fix. | Yes |
Note | A potential issue. Strive to fix. | More or less |
result | ||
---|---|---|
Error | ||
Warning | ||
Note |
type | file | run |
---|---|---|
test | test-*.R |
alphabetical order |
helper | helper-*.R |
before tests, from load_all() |
setup | setup-*.R |
before tests, not from load_all() |
teardown teardown-*.R |
after tests |
type | file | run |
---|---|---|
test | test-*.R |
alphabetical order |
helper | helper-*.R |
before tests, from load_all() |
setup | setup-*.R |
before tests, not from load_all() |
teardown teardown-*.R |
after tests |
tests/testthat/
test-count-donations.R
and test-tables.R
use donations_test_data
. Let's move it to a helper file. First, create the file with fs::file_create("tests/testthat/helper-donations_data.R")
. Open it manually or use edit_file()
.donations_test_data
into helper-donations_data.R
.donations_test_data
code from the two test files.fs::file_create("tests/testthat/helper-donations_data.R")
tests/testthat/helper-donations_data.R
donations_test_data <- data.frame( donor_id = 1:15, sector = c(7L, 2L, 8L, 6L, 5L, 5L, 8L, 1L, 5L, 4L, 4L, 3L, 7L, 5L, 8L), donation = c( 529.58, 16.64, 410.88, 448.73, 211.62, 642.53, 410.93, 707.38, 30.19, 573.02, 286.31, 734.73, 971.81, 30, 465.92 ))
use_travis()
, use_appveyor()
, etcuse_github_actions_*()
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |