Skip to content

Commit 2f1f304

Browse files
committed
working reporter with payload like ipython_nose
1 parent e4b4eef commit 2f1f304

1 file changed

Lines changed: 73 additions & 33 deletions

File tree

R-projects-test-example.ipynb

Lines changed: 73 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,12 @@
2626
},
2727
{
2828
"cell_type": "code",
29-
"execution_count": 93,
29+
"execution_count": null,
3030
"metadata": {},
3131
"outputs": [],
3232
"source": [
3333
"library(testthat)\n",
3434
"\n",
35-
"comm <- IRkernel::comm_manager()$new_comm('dc_project')\n",
36-
"comm$open()\n",
37-
"comm$send(list(a = 'hey'))\n",
38-
"\n",
3935
"ProjectReporter <- R6::R6Class(\"ProjectReporter\", inherit = testthat::ListReporter,\n",
4036
" public = list(\n",
4137
" all_tests = NULL,\n",
@@ -45,45 +41,89 @@
4541
" self$comm <- comm\n",
4642
" self$all_tests = testthat:::Stack$new()\n",
4743
" },\n",
48-
" end_test = function(context, test) {\n",
49-
" out_env <- super$end_test(context, test)\n",
50-
" jsonable <- lapply(out_env$as_list(), `[`, c('message', 'test'))\n",
51-
" self$comm$send(jsonable)\n",
52-
" out_env\n",
53-
" #assign('output', out, envir=globalenv())\n",
54-
" \n",
44+
" get_results = function() {\n",
45+
" test_res <- lapply(self$results$as_list(),\n",
46+
" self$dump_test)\n",
47+
" test_df <- data.frame(do.call(rbind, test_res))\n",
48+
" \n",
49+
" # summarize number of tests, etc..\n",
50+
" summary <- list(\n",
51+
" tests = length(test_res),\n",
52+
" failures = sum(test_df$outcome == 'fail'),\n",
53+
" errors = sum(test_df$outcome == 'error')\n",
54+
" )\n",
55+
"\n",
56+
" payload <- list(\n",
57+
" success = all(as.logical(test_df$success)),\n",
58+
" summary = summary,\n",
59+
" tests = test_res\n",
60+
" )\n",
61+
" \n",
62+
" self$comm$send(payload)\n",
63+
"\n",
64+
" payload\n",
65+
" },\n",
66+
" dump_test = function(test) {\n",
67+
" message <- paste(\n",
68+
" lapply(test$results, `[[`, 'message'),\n",
69+
" collapse = '\\n')\n",
70+
"\n",
71+
" res <- testthat:::sumarize_one_test_results(test)\n",
72+
" success <- !any(res$failed, res$error)\n",
73+
" # figure out outcome, e.g. for counting errors later\n",
74+
" if (!success) {\n",
75+
" outcome <- if (res$failed) 'fail' else 'error'\n",
76+
" } else outcome <- 'success'\n",
77+
"\n",
78+
" list(name = test$test,\n",
79+
" message = message,\n",
80+
" success = !any(res$failed, res$error),\n",
81+
" outcome = outcome)\n",
5582
" }\n",
5683
" ),\n",
5784
" private = list(\n",
5885
" )\n",
59-
")"
86+
")\n",
87+
"\n",
88+
".run_tests <- function(test_expr) {\n",
89+
" comm <- IRkernel::comm_manager()$new_comm('dc_project')\n",
90+
" comm$open()\n",
91+
"\n",
92+
" reporter <- ProjectReporter$new(comm = comm)\n",
93+
" reporter$start_file('some name')\n",
94+
" env = test_env()\n",
95+
" tests <- substitute(test_expr)\n",
96+
" with_reporter(\n",
97+
" reporter = reporter, start_end_reporter = TRUE,\n",
98+
" eval(tests, envir = env)\n",
99+
" )\n",
100+
" reporter\n",
101+
"}"
60102
]
61103
},
62104
{
63105
"cell_type": "code",
64-
"execution_count": 94,
106+
"execution_count": null,
65107
"metadata": {},
66108
"outputs": [],
67109
"source": [
68-
"reporter <- ProjectReporter$new(comm = comm)\n",
69-
"env = test_env()\n",
70-
"reporter$start_file('some name')\n",
71-
"with_reporter(reporter = reporter, start_end_reporter = TRUE,\n",
72-
" eval(\n",
73-
" {\n",
74-
" test_that(\"a passing test\", {\n",
75-
" expect_equal(2, 2)\n",
76-
" expect_equal(3, 3)\n",
77-
" })\n",
78-
" \n",
79-
" test_that(\"a failing test\", {\n",
80-
" expect_equal(2, 3)\n",
81-
" expect_equal(3, 4)\n",
82-
" })\n",
83-
" },\n",
84-
" envir = env\n",
85-
" )\n",
86-
")\n"
110+
"out <- .run_tests({\n",
111+
" test_that(\"a passing test\", {\n",
112+
" expect_equal(2, 2)\n",
113+
" expect_equal(3, 3)\n",
114+
" })\n",
115+
"\n",
116+
" test_that(\"a failing test\", {\n",
117+
" expect_equal(2, 3)\n",
118+
" expect_equal(3, 4)\n",
119+
" })\n",
120+
" \n",
121+
" test_that(\"an erroring test\", {\n",
122+
" stopifnot(FALSE)\n",
123+
" expect_equat(2, 2)\n",
124+
" })\n",
125+
"})\n",
126+
"out$get_results()"
87127
]
88128
}
89129
],

0 commit comments

Comments
 (0)