Skip to content

Assignment in expect_no_warning() does not work if a warning was thrown #1991

Open
@lschneiderbauer

Description

@lschneiderbauer

expect_no_warning() seems to behave somewhat inconsistent to the rest of the family (e.g. compared to expect_warning()): Assignment inside the body fails if a warning was thrown.

There was a similar discussion in #998 where this was deemed a bug.

Note the second try, variable b: it's the only case where assignment does not work, in all other cases it works.

library(testthat)
foo_warning <- function() {
  warning("a warning")
  5
}
foo <- function() {
  5
}

expect_warning(a <- foo_warning())
a
#> [1] 5

expect_no_warning(b <- foo_warning())
#> Error: Expected `b <- foo_warning()` to run without any warnings.
#> ℹ Actually got a <simpleWarning> with text:
#>   a warning
b
#> Error: Objekt 'b' nicht gefunden

expect_warning(c <- foo())
#> Error: `c <- foo()` did not produce any warnings.
c
#> [1] 5

expect_no_warning(d <- foo())
d
#> [1] 5

Created on 2024-10-04 with reprex v2.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorconditions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions