Skip to content

as.expectation.logical() missing message in case of failure for testthat >=2.0 #836

@zappingseb

Description

@zappingseb

This might be related to issue #693

For testing our packages we are double checking the message from as.expectation.logical and printing it out by

rec <- 2
exp <- 2
expect_equal(
  rec,
  exp,
  message=glue("Expect equal: \"Received\":\"{rec}\", \"Expected\":\"{exp}\"")
)

and basically parse the information given in the message to show it to the users in a test report. We can create a table with the info:

data.frame(
   success  = "expectation_success" %in% class(expec),
   received = stringr::str_extract(expec$message,"(?<=Received\"\\:\")[^\\,]*(?=\\\")")
   expected = stringr::str_extract(expec$message,"(?<=Expected\"\\:\")[^\\,]*(?=\\\")")
)
>success  received expected
>   TRUE        2        2

This went great in testthat <2.0. Now the expect as.expectation.logical destroys the message in case of success.

as.expectation.logical <- function(x, message, ..., srcref = NULL, info = NULL) {
  type <- if (x) "success" else "failure"
  message <- if (x) "success" else add_info(message, info)
  expectation(type, message, srcref = srcref)
}

we just receive

>success  received expected
>   TRUE      ""       ""

Could you plugin an option into the API to always give the failure message? Or is there a way to put our message anywhere else, e.g. into srcref?

A proposal by the RTest team is to provide an option such as allMessages:

as.expectation.logical <- function(x, message, ..., srcref = NULL, info = NULL) {
  type <- if (x) "success" else "failure"
  message <- if (x && !getOption("allMessages", default = FALSE)) "success" else add_info(message, info)
  expectation(type, message, srcref = srcref)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions