Skip to content

Commit 2cd3a30

Browse files
authored
Merge pull request #1043 from stan-dev/reduce-test-output
Reduce console output in tests
2 parents 5a6d780 + 491845a commit 2cd3a30

13 files changed

+149
-84
lines changed

tests/testthat/test-fit-init.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set_cmdstan_path()
55
data_list_schools <- testing_data("schools")
66
data_list_logistic <- testing_data("logistic")
77
test_inits <- function(mod, fit_init, data_list = NULL) {
8+
utils::capture.output({
89
fit_sample <- mod$sample(data = data_list, chains = 1, init = fit_init,
910
iter_sampling = 100, iter_warmup = 100, refresh = 0, seed = 1234)
1011
fit_sample_multi <- mod$sample(data = data_list, chains = 5, init = fit_init,
@@ -20,6 +21,7 @@ test_inits <- function(mod, fit_init, data_list = NULL) {
2021
draws = posterior::as_draws_rvars(fit_init$draws())
2122
fit_sample_draws <- mod$sample(data = data_list, chains = 1, init = draws,
2223
iter_sampling = 100, iter_warmup = 100, refresh = 0, seed = 1234)
24+
})
2325
return(0)
2426
}
2527

tests/testthat/test-fit-laplace.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
context("fitted-vb")
1+
context("fitted-laplace")
22

33
set_cmdstan_path()
44
fit_laplace <- testing_fit("logistic", method = "laplace", seed = 100)

tests/testthat/test-fit-shared.R

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -454,23 +454,27 @@ test_that("draws are returned for model with spaces", {
454454
test_that("sampling with inits works with include_paths", {
455455
stan_program_w_include <- testing_stan_file("bernoulli_include")
456456
exe <- cmdstan_ext(strip_ext(stan_program_w_include))
457-
if(file.exists(exe)) {
457+
if (file.exists(exe)) {
458458
file.remove(exe)
459459
}
460460

461-
mod_w_include <- cmdstan_model(stan_file = stan_program_w_include, quiet = FALSE,
462-
include_paths = test_path("resources", "stan"))
461+
mod_w_include <- cmdstan_model(stan_file = stan_program_w_include,
462+
include_paths = test_path("resources", "stan"))
463463

464464
data_list <- list(N = 10, y = c(0,1,0,0,0,0,0,0,0,1))
465-
466-
fit <- mod_w_include$sample(
467-
data = data_list,
468-
seed = 123,
469-
chains = 4,
470-
parallel_chains = 4,
471-
refresh = 500,
472-
init = list(list(theta = 0.25), list(theta = 0.25), list(theta = 0.25), list(theta = 0.25))
473-
)
465+
expect_no_error(utils::capture.output(
466+
fit <- mod_w_include$sample(
467+
data = data_list,
468+
seed = 123,
469+
chains = 4,
470+
parallel_chains = 4,
471+
refresh = 500,
472+
init = list(list(theta = 0.25),
473+
list(theta = 0.25),
474+
list(theta = 0.25),
475+
list(theta = 0.25))
476+
)
477+
))
474478
})
475479

476480
test_that("CmdStanModel created with exe_file works", {
@@ -548,8 +552,12 @@ test_that("code() warns if model not created with Stan file", {
548552
stan_program <- testing_stan_file("bernoulli")
549553
mod <- testing_model("bernoulli")
550554
mod_exe <- cmdstan_model(exe_file = mod$exe_file())
551-
fit_exe <- mod_exe$sample(data = list(N = 10, y = c(0, 1, 0, 1, 0, 1, 0, 1, 0, 1)),
552-
refresh = 0)
555+
utils::capture.output(
556+
fit_exe <- mod_exe$sample(
557+
data = list(N = 10, y = c(0, 1, 0, 1, 0, 1, 0, 1, 0, 1)),
558+
refresh = 0
559+
)
560+
)
553561
expect_warning(
554562
expect_null(fit_exe$code()),
555563
"'$code()' will return NULL because the 'CmdStanModel' was not created with a Stan file",

tests/testthat/test-model-compile.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ test_that("dirname of stan_file is used as include path if no other paths suppli
837837

838838
mod_tmp <- cmdstan_model(stan_file, compile = FALSE)
839839
expect_true(mod_tmp$check_syntax())
840-
expect_true(mod_tmp$format())
840+
utils::capture.output(expect_true(mod_tmp$format()))
841841
expect_s3_class(mod_tmp$compile(), "CmdStanModel")
842842
})
843843

tests/testthat/test-model-expose-functions.R

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@ stan_prog <- paste(function_decl,
8080
model <- write_stan_file(stan_prog)
8181
data_list <- testing_data("bernoulli")
8282
mod <- cmdstan_model(model, force_recompile = TRUE)
83-
fit <- mod$sample(data = data_list)
83+
utils::capture.output(
84+
fit <- mod$sample(data = data_list)
85+
)
8486

8587

8688
test_that("Functions can be exposed in model object", {
87-
expect_no_error(mod$expose_functions(verbose = TRUE))
89+
expect_no_error(mod$expose_functions())
8890
})
8991

9092

@@ -260,7 +262,7 @@ test_that("Functions handle complex types correctly", {
260262
})
261263

262264
test_that("Functions can be exposed in fit object", {
263-
fit$expose_functions(verbose = TRUE)
265+
fit$expose_functions()
264266

265267
expect_equal(
266268
fit$functions$rtn_vec(c(1,2,3,4)),
@@ -284,7 +286,9 @@ test_that("Compiled functions can be copied to global environment", {
284286

285287
test_that("Functions can be compiled with model", {
286288
mod <- cmdstan_model(model, force_recompile = TRUE, compile_standalone = TRUE)
287-
fit <- mod$sample(data = data_list)
289+
utils::capture.output(
290+
fit <- mod$sample(data = data_list)
291+
)
288292

289293
expect_message(
290294
fit$expose_functions(),
@@ -344,9 +348,11 @@ test_that("rng functions can be exposed", {
344348
model <- write_stan_file(stan_prog)
345349
data_list <- testing_data("bernoulli")
346350
mod <- cmdstan_model(model, force_recompile = TRUE)
347-
fit <- mod$sample(data = data_list)
351+
utils::capture.output(
352+
fit <- mod$sample(data = data_list)
353+
)
348354

349-
fit$expose_functions(verbose = TRUE)
355+
fit$expose_functions()
350356
set.seed(10)
351357
res1_1 <- fit$functions$wrap_normal_rng(5,10)
352358
res2_1 <- fit$functions$wrap_normal_rng(5,10)

tests/testthat/test-model-init.R

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,12 @@ test_that("Initial values for single-element containers treated correctly", {
303303
"
304304
mod <- cmdstan_model(write_stan_file(modcode), force_recompile = TRUE)
305305
expect_no_error(
306-
fit <- mod$sample(
307-
data = list(y_mean = 0),
308-
init = list(list(y = c(0))),
309-
chains = 1
306+
utils::capture.output(
307+
fit <- mod$sample(
308+
data = list(y_mean = 0),
309+
init = list(list(y = c(0))),
310+
chains = 1
311+
)
310312
)
311313
)
312314
})
@@ -331,7 +333,13 @@ test_that("Pathfinder inits do not drop dimensions", {
331333
"
332334
mod <- cmdstan_model(write_stan_file(modcode), force_recompile = TRUE)
333335
data <- list(N = 100, y = rnorm(100))
334-
pf <- mod$pathfinder(data = data, psis_resample = FALSE)
335-
expect_no_error(fit <- mod$sample(data = data, init = pf, chains = 1,
336-
iter_warmup = 100, iter_sampling = 100))
337-
})
336+
utils::capture.output(
337+
pf <- mod$pathfinder(data = data, psis_resample = FALSE)
338+
)
339+
expect_no_error(
340+
utils::capture.output(
341+
fit <- mod$sample(data = data, init = pf, chains = 1,
342+
iter_warmup = 100, iter_sampling = 100)
343+
)
344+
)
345+
})

tests/testthat/test-model-laplace.R

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ test_that("laplace() method errors for any invalid argument before calling cmdst
3636
for (nm in names(bad_arg_values)) {
3737
args <- ok_arg_values
3838
args[[nm]] <- bad_arg_values[[nm]]
39-
expect_error(do.call(mod$laplace, args), regexp = nm, info = nm)
39+
utils::capture.output(
40+
expect_error(do.call(mod$laplace, args), regexp = nm, info = nm)
41+
)
4042
}
4143
args <- ok_arg_values
4244
args$opt_args <- list(iter = "NOT_A_NUMBER")
@@ -63,10 +65,12 @@ test_that("laplace() runs when all arguments specified validly", {
6365
})
6466

6567
test_that("laplace() all valid 'mode' inputs give same results", {
66-
mode <- mod$optimize(data = data_list, jacobian = TRUE, seed = 100, refresh = 0)
67-
fit1 <- mod$laplace(data = data_list, mode = mode, seed = 100, refresh = 0)
68-
fit2 <- mod$laplace(data = data_list, mode = mode$output_files(), seed = 100, refresh = 0)
69-
fit3 <- mod$laplace(data = data_list, mode = NULL, seed = 100, refresh = 0)
68+
utils::capture.output({
69+
mode <- mod$optimize(data = data_list, jacobian = TRUE, seed = 100, refresh = 0)
70+
fit1 <- mod$laplace(data = data_list, mode = mode, seed = 100, refresh = 0)
71+
fit2 <- mod$laplace(data = data_list, mode = mode$output_files(), seed = 100, refresh = 0)
72+
fit3 <- mod$laplace(data = data_list, mode = NULL, seed = 100, refresh = 0)
73+
})
7074

7175
expect_is(fit1, "CmdStanLaplace")
7276
expect_is(fit2, "CmdStanLaplace")
@@ -85,17 +89,22 @@ test_that("laplace() all valid 'mode' inputs give same results", {
8589
})
8690

8791
test_that("laplace() allows choosing number of draws", {
88-
fit <- mod$laplace(data = data_list, draws = 10, refresh = 0)
92+
utils::capture.output({
93+
fit <- mod$laplace(data = data_list, draws = 10, refresh = 0)
94+
fit2 <- mod$laplace(data = data_list, draws = 100, refresh = 0)
95+
})
96+
8997
expect_equal(fit$metadata()$draws, 10)
9098
expect_equal(posterior::ndraws(fit$draws()), 10)
9199

92-
fit2 <- mod$laplace(data = data_list, draws = 100, refresh = 0)
93100
expect_equal(fit2$metadata()$draws, 100)
94101
expect_equal(posterior::ndraws(fit2$draws()), 100)
95102
})
96103

97104
test_that("laplace() errors if jacobian arg doesn't match what optimize used", {
98-
fit <- mod$optimize(data = data_list, jacobian = FALSE, refresh = 0)
105+
utils::capture.output(
106+
fit <- mod$optimize(data = data_list, jacobian = FALSE, refresh = 0)
107+
)
99108
expect_error(
100109
mod$laplace(data = data_list, mode = fit, jacobian = TRUE),
101110
"'jacobian' argument to optimize and laplace must match"
@@ -107,7 +116,9 @@ test_that("laplace() errors if jacobian arg doesn't match what optimize used", {
107116
})
108117

109118
test_that("laplace() errors with bad combinations of arguments", {
110-
fit <- mod$optimize(data = data_list, jacobian = TRUE, refresh = 0)
119+
utils::capture.output(
120+
fit <- mod$optimize(data = data_list, jacobian = TRUE, refresh = 0)
121+
)
111122
expect_error(
112123
mod$laplace(data = data_list, mode = mod, opt_args = list(iter = 10)),
113124
"Cannot specify both 'opt_args' and 'mode' arguments."
@@ -120,14 +131,15 @@ test_that("laplace() errors with bad combinations of arguments", {
120131

121132
test_that("laplace() errors if optimize() fails", {
122133
mod_schools <- testing_model("schools")
123-
expect_error(
124-
expect_warning(
125-
expect_message(
126-
mod_schools$laplace(data = testing_data("schools"), refresh = 0),
127-
"Line search failed to achieve a sufficient decrease"
134+
utils::capture.output(
135+
expect_error(
136+
expect_warning(
137+
expect_message(
138+
mod_schools$laplace(data = testing_data("schools"), refresh = 0),
139+
"Line search failed to achieve a sufficient decrease"
140+
),
141+
"Fitting finished unexpectedly"
128142
),
129-
"Fitting finished unexpectedly"
130-
),
131-
"Optimization failed"
132-
)
143+
"Optimization failed"
144+
))
133145
})

0 commit comments

Comments
 (0)