|
26 | 26 | }, |
27 | 27 | { |
28 | 28 | "cell_type": "code", |
29 | | - "execution_count": 93, |
| 29 | + "execution_count": null, |
30 | 30 | "metadata": {}, |
31 | 31 | "outputs": [], |
32 | 32 | "source": [ |
33 | 33 | "library(testthat)\n", |
34 | 34 | "\n", |
35 | | - "comm <- IRkernel::comm_manager()$new_comm('dc_project')\n", |
36 | | - "comm$open()\n", |
37 | | - "comm$send(list(a = 'hey'))\n", |
38 | | - "\n", |
39 | 35 | "ProjectReporter <- R6::R6Class(\"ProjectReporter\", inherit = testthat::ListReporter,\n", |
40 | 36 | " public = list(\n", |
41 | 37 | " all_tests = NULL,\n", |
|
45 | 41 | " self$comm <- comm\n", |
46 | 42 | " self$all_tests = testthat:::Stack$new()\n", |
47 | 43 | " },\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", |
55 | 82 | " }\n", |
56 | 83 | " ),\n", |
57 | 84 | " private = list(\n", |
58 | 85 | " )\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 | + "}" |
60 | 102 | ] |
61 | 103 | }, |
62 | 104 | { |
63 | 105 | "cell_type": "code", |
64 | | - "execution_count": 94, |
| 106 | + "execution_count": null, |
65 | 107 | "metadata": {}, |
66 | 108 | "outputs": [], |
67 | 109 | "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()" |
87 | 127 | ] |
88 | 128 | } |
89 | 129 | ], |
|
0 commit comments