Skip to content

How to check both output and an error?Β #2274

@MichaelChirico

Description

@MichaelChirico

I have some code that works like this:

check <- function(...) {
  writeLines(c(...))
  stop(...)
}

The idea is that because the stop() buffer is pretty limited, I opt to dump a potentially large amount of information with writeLines(), then follow up by signalling an error like "Failed; see above".

I expected {testthat} code like the following to work:

expect_error(
  expect_output(
    check(...),
    "outputMatch"
  ),
  "errorMatch"
)

And I thought all was well, however, now I'm noticing this isn't working like I'd intended at all. Basically, I'm not sure if expect_output() is being run at all. Observe:

expect_error(
  expect_output(
    { cat("foo\n"); stop("bar") },
    "xxx" # not a match
  ),
  "bar" # a match
)
# passes, no error from `expect_output()` failing!

Somewhat more insidiously, we don't even get notified if we've totally botched the input to expect_output():

expect_error(
  expect_output(
    { cat("foo\n"); stop("bar") },
    oopsidaisy() # not defined!
  ),
  "bar" # a match
)
# passes, no error from `expect_output()` failing!

(In some sense, that's the same issue as above, since abstractly both are errors from the expect_output() expression, albeit with different condition classes)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions