Skip to content

test_that does not detect correctly a test failure or success linked with warning classesΒ #2242

@afinez

Description

@afinez

Hi all,

When test_that() contains a failing test linked with warning classes, it seems that the test passes.

expect_warning(rlang::warn(class = "C1"), class = "C2")

provides the expected error:

Avis : Erreur : `rlang::warn(class = "C1")` did not throw a warning with class <C2>.

and yet, including it in test_that() does not result in a test fail

test_that("a C2 warning is expected",
          {
            expect_warning(rlang::warn(class = "C1"),  class = "C2")
          })

--> Test passed 🌈

Symmetrically, when testing the absence of a warning of class C2 with expect_no_warning , the expected result is produced (no error)

 expect_no_warning(
              rlang::warn(class = "C1"),
              class = "C2"
            )

and yet, including it test_that

test_that("no C2 warning is expected",
          {
            expect_no_warning(
              rlang::warn(class = "C1"),
              class = "C2"
            )
          })

generates this unexpected console output:

── Warning: no C2 warning is expected ───────────────────────

Backtrace:
    β–†
 1. └─testthat::expect_no_warning(rlang::warn(class = "C1"), class = "C2")
 2.   └─testthat:::expect_no_(...)
 3.     └─testthat:::quasi_capture(enquo(object), NULL, capture)
 4.       β”œβ”€testthat (local) .capture(...)
 5.       β”‚ └─rlang::try_fetch(...)
 6.       β”‚   └─base::withCallingHandlers(...)
 7.       └─rlang::eval_bare(quo_get_expr(.quo), quo_get_env(.quo))

I guess the test is a success though, since the function output is TRUE, but this console backtrace may be inappropriate.

Is this expected ?
NB: I saw that a close issue was declared #1894 for edition 2, so I am using local_edition(3) here

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