Skip to content

Commit 96fef25

Browse files
committed
hello, reporter world!
1 parent 2f1f304 commit 96fef25

10 files changed

Lines changed: 230 additions & 147 deletions

.Rbuildignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
^.*\.Rproj$
2+
^\.Rproj\.user$
3+
.*\.swp
4+
\.ipynb_checkpoints

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData
4+
.Ruserdata
5+
6+
# vim swapfiles
7+
*.swp
8+
9+
# ipython notebook checkpoints
10+
.ipynb_checkpoints

DESCRIPTION

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Package: IRkernel.testthat
2+
Title: What the Package Does (one line, title case)
3+
Version: 0.0.1
4+
Authors@R: person("Michael", "Chow", email = "michael@datacamp.com", role = c("aut", "cre"))
5+
Description: Implements reporter for communicating testthat results to a jupyter client.
6+
License: All Rights Reserved
7+
Depends: R (>= 3.3.1)
8+
Imports:
9+
testthat,
10+
IRkernel
11+
Encoding: UTF-8
12+
LazyData: true
13+
RoxygenNote: 6.0.1

IRkernel.testthat.Rproj

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Version: 1.0
2+
3+
RestoreWorkspace: No
4+
SaveWorkspace: No
5+
AlwaysSaveHistory: Default
6+
7+
EnableCodeIndexing: Yes
8+
UseSpacesForTab: Yes
9+
NumSpacesForTab: 2
10+
Encoding: UTF-8
11+
12+
RnwWeave: Sweave
13+
LaTeX: pdfLaTeX
14+
15+
AutoAppendNewline: Yes
16+
StripTrailingWhitespace: Yes
17+
18+
BuildType: Package
19+
PackageUseDevtools: Yes
20+
PackageInstallArgs: --no-multiarch --with-keep.source
21+
PackageRoxygenize: rd,collate,namespace

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Generated by roxygen2: do not edit by hand
2+
3+
export(ProjectReporter)
4+
export(run_tests)

R-projects-test-example.ipynb

Lines changed: 0 additions & 147 deletions
This file was deleted.

R/reporter-project.R

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#' Project reporter: send test results to IRkernel, for use with DataCamp Projects.
2+
#'
3+
#' This reporter subclasses the ListReporter, to send detailed test information for
4+
#' use with jupyter notebooks.
5+
#'
6+
#' @export
7+
ProjectReporter <- R6::R6Class("ProjectReporter", inherit = testthat::ListReporter,
8+
public = list(
9+
all_tests = NULL,
10+
comm = NULL,
11+
initialize = function(..., comm) {
12+
super$initialize(...)
13+
self$comm <- comm
14+
self$all_tests = testthat:::Stack$new()
15+
},
16+
end_reporter = function() {
17+
test_res <- lapply(self$results$as_list(),
18+
self$dump_test)
19+
test_df <- data.frame(do.call(rbind, test_res))
20+
21+
# summarize number of tests, etc..
22+
summary <- list(
23+
tests = length(test_res),
24+
failures = sum(test_df$outcome == 'fail'),
25+
errors = sum(test_df$outcome == 'error')
26+
)
27+
28+
payload <- list(
29+
success = all(as.logical(test_df$success)),
30+
summary = summary,
31+
tests = test_res
32+
)
33+
34+
self$comm$send(payload)
35+
36+
payload
37+
},
38+
dump_test = function(test) {
39+
message <- paste(
40+
lapply(test$results, `[[`, 'message'),
41+
collapse = '\n')
42+
43+
res <- testthat:::sumarize_one_test_results(test)
44+
success <- !any(res$failed, res$error)
45+
# figure out outcome, e.g. for counting errors later
46+
if (!success) {
47+
outcome <- if (res$failed) 'fail' else 'error'
48+
} else outcome <- 'success'
49+
50+
list(name = test$test,
51+
message = message,
52+
success = !any(res$failed, res$error),
53+
outcome = outcome)
54+
}
55+
),
56+
private = list(
57+
)
58+
)
59+
60+
#' Creates a project reporter and executes tests.
61+
#'
62+
#' @param test_expr testthat code to execute
63+
#'
64+
#' @export
65+
run_tests <- function(test_expr) {
66+
comm <- IRkernel::comm_manager()$new_comm('dc_project')
67+
comm$open()
68+
69+
reporter <- ProjectReporter$new(comm = comm)
70+
reporter$start_file('some name')
71+
env = testthat::test_env()
72+
tests <- substitute(test_expr)
73+
testthat::with_reporter(
74+
reporter = reporter, start_end_reporter = TRUE,
75+
eval(tests, envir = env)
76+
)
77+
reporter
78+
}

inst/R-projects-test-example.ipynb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"Checking Projects with testthat\n",
8+
"-----------------------------\n",
9+
"\n",
10+
"In this notebook, a testthat reporter sends test output to the jupyter client.\n",
11+
"To see this in action, paste this javascript into your console:\n",
12+
"\n",
13+
"```\n",
14+
"Jupyter.notebook.kernel.comm_manager.register_target('dc_project',\n",
15+
" function(comm, msg) {\n",
16+
" // comm is the frontend comm instance\n",
17+
" // msg is the comm_open message, which can carry data\n",
18+
"\n",
19+
" // Register handlers for later messages:\n",
20+
" comm.on_msg(function(msg) { console.log('msg:', msg)} );\n",
21+
" comm.on_close(function(msg) { console.log('close msg:', msg)});\n",
22+
" //comm.send({'foo': 0});\n",
23+
" })\n",
24+
"```"
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": null,
30+
"metadata": {},
31+
"outputs": [],
32+
"source": [
33+
"# TODO: load testthat funcs into an envir, or make parent?\n",
34+
"library(testthat)\n",
35+
"IRkernel.testthat::run_tests({\n",
36+
" test_that(\"a passing test\", {\n",
37+
" expect_equal(2, 2)\n",
38+
" expect_equal(3, 3)\n",
39+
" })\n",
40+
"\n",
41+
" test_that(\"a failing test\", {\n",
42+
" expect_equal(2, 3)\n",
43+
" expect_equal(3, 4)\n",
44+
" })\n",
45+
" \n",
46+
" test_that(\"an erroring test\", {\n",
47+
" stopifnot(FALSE)\n",
48+
" expect_equat(2, 2)\n",
49+
" })\n",
50+
"})"
51+
]
52+
}
53+
],
54+
"metadata": {
55+
"kernelspec": {
56+
"display_name": "R",
57+
"language": "R",
58+
"name": "ir"
59+
},
60+
"language_info": {
61+
"codemirror_mode": "r",
62+
"file_extension": ".r",
63+
"mimetype": "text/x-r-source",
64+
"name": "R",
65+
"pygments_lexer": "r",
66+
"version": "3.3.1"
67+
}
68+
},
69+
"nbformat": 4,
70+
"nbformat_minor": 2
71+
}

man/ProjectReporter.Rd

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/run_tests.Rd

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)