rtestthat

Get more useful output from testthat::expect_equal()


In the following example, the actual vector is missing two elements: OH1 and PA-PPL2. Is there a way to get more useful output than what expect_equal() gives here? I'd like to know which items are missing from actual, not just that the counts are different.

library(testthat)

test_that(
  'files are named correctly',
  {
    expected <- c(AMEREN1 = "AMEREN24.01.15_March-1_DM_RL.pdf", COMED1 = "COMED24.01.15_March-1_DM_RL.pdf", 
                  `MD-OTH1` = "MD-OTH24.01.15_March_DM_RL.pdf", MD1 = "MD24.01.15_March_DM_RL.pdf", 
                  NJ1 = "NJ24.01.15_March_DM_RL.pdf", OH1 = "OH24.01.15_March_DM_RL.pdf", 
                  PA1 = "PA24.01.15_March_DM_RL.pdf", AMEREN2 = "AMEREN24.01.15_March-2_DM_RL.pdf", 
                  COMED2 = "COMED24.01.15_March-2_DM_RL.pdf", `PA-DUQ2` = "PA24.01.15_March-DUQ_DM_RL.pdf", 
                  `PA-METED2` = "PA24.01.15_March-METED_DM_RL.pdf", `PA-PECO2` = "PA24.01.15_March-PECO_DM_RL.pdf", 
                  `PA-PENELEC2` = "PA24.01.15_March-PENELEC_DM_RL.pdf", `PA-PENN2` = "PA24.01.15_March-PENN_DM_RL.pdf", 
                  `PA-PPL2` = "PA24.01.15_March-PPL_DM_RL.pdf", `PA-WPP2` = "PA24.01.15_March-WPP_DM_RL.pdf"
    )
    actual <- c(AMEREN1 = "AMEREN24.01.15_March-1_DM_RL.pdf", COMED1 = "COMED24.01.15_March-1_DM_RL.pdf", 
                `MD-OTH1` = "MD-OTH24.01.15_March_DM_RL.pdf", MD1 = "MD24.01.15_March_DM_RL.pdf", 
                NJ1 = "NJ24.01.15_March_DM_RL.pdf", PA1 = "PA24.01.15_March_DM_RL.pdf", 
                AMEREN2 = "AMEREN24.01.15_March-2_DM_RL.pdf", COMED2 = "COMED24.01.15_March-2_DM_RL.pdf", 
                `PA-DUQ2` = "PA24.01.15_March-DUQ_DM_RL.pdf", `PA-METED2` = "PA24.01.15_March-METED_DM_RL.pdf", 
                `PA-PECO2` = "PA24.01.15_March-PECO_DM_RL.pdf", `PA-PENELEC2` = "PA24.01.15_March-PENELEC_DM_RL.pdf", 
                `PA-PENN2` = "PA24.01.15_March-PENN_DM_RL.pdf", `PA-WPP2` = "PA24.01.15_March-WPP_DM_RL.pdf"
    )
    
    expect_equal(
      actual,
      expected
    )
  }
)
#> -- Failure ('<text>:24'): files are named correctly ----------------------------
#> `actual` not equal to `expected`.
#> Lengths differ: 14 is not 16
#> Error in `reporter$stop_if_needed()`:
#> ! Test failed
#> Backtrace:
#>      x
#>   1. \-testthat::test_that(...)
#>   2.   \-withr (local) `<fn>`(`<env>`)
#>   3.     +-base::tryCatch(...)
#>   4.     | \-base (local) tryCatchList(expr, classes, parentenv, handlers)
#>   5.     |   \-base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>   6.     |     \-base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   7.     \-base::eval(handler$expr, handler$envir)
#>   8.       \-base::eval(handler$expr, handler$envir)
#>   9.         \-reporter$stop_if_needed()
#>  10.           \-rlang::abort("Test failed")

Created on 2023-12-26 with reprex v2.0.2


Solution

  • In {testthat} 3rd edition, the expect_equal() comparison is done with waldo::compare(), which will give more details. You are probably using {testthat} 2nd edition, where expect_equal() uses all.equal().

    You can temporarily switch to the 3rd edition for this test with:

    testthat::local_edition(3)
    

    or activate it globally as described here.

    With a the reprex:

    library(testthat)
    
    test_that(
      'files are named correctly',
      {
        local_edition(3)
        expected <- c(AMEREN1 = "AMEREN24.01.15_March-1_DM_RL.pdf", COMED1 = "COMED24.01.15_March-1_DM_RL.pdf", 
                      `MD-OTH1` = "MD-OTH24.01.15_March_DM_RL.pdf", MD1 = "MD24.01.15_March_DM_RL.pdf", 
                      NJ1 = "NJ24.01.15_March_DM_RL.pdf", OH1 = "OH24.01.15_March_DM_RL.pdf", 
                      PA1 = "PA24.01.15_March_DM_RL.pdf", AMEREN2 = "AMEREN24.01.15_March-2_DM_RL.pdf", 
                      COMED2 = "COMED24.01.15_March-2_DM_RL.pdf", `PA-DUQ2` = "PA24.01.15_March-DUQ_DM_RL.pdf", 
                      `PA-METED2` = "PA24.01.15_March-METED_DM_RL.pdf", `PA-PECO2` = "PA24.01.15_March-PECO_DM_RL.pdf", 
                      `PA-PENELEC2` = "PA24.01.15_March-PENELEC_DM_RL.pdf", `PA-PENN2` = "PA24.01.15_March-PENN_DM_RL.pdf", 
                      `PA-PPL2` = "PA24.01.15_March-PPL_DM_RL.pdf", `PA-WPP2` = "PA24.01.15_March-WPP_DM_RL.pdf"
        )
        actual <- c(AMEREN1 = "AMEREN24.01.15_March-1_DM_RL.pdf", COMED1 = "COMED24.01.15_March-1_DM_RL.pdf", 
                    `MD-OTH1` = "MD-OTH24.01.15_March_DM_RL.pdf", MD1 = "MD24.01.15_March_DM_RL.pdf", 
                    NJ1 = "NJ24.01.15_March_DM_RL.pdf", PA1 = "PA24.01.15_March_DM_RL.pdf", 
                    AMEREN2 = "AMEREN24.01.15_March-2_DM_RL.pdf", COMED2 = "COMED24.01.15_March-2_DM_RL.pdf", 
                    `PA-DUQ2` = "PA24.01.15_March-DUQ_DM_RL.pdf", `PA-METED2` = "PA24.01.15_March-METED_DM_RL.pdf", 
                    `PA-PECO2` = "PA24.01.15_March-PECO_DM_RL.pdf", `PA-PENELEC2` = "PA24.01.15_March-PENELEC_DM_RL.pdf", 
                    `PA-PENN2` = "PA24.01.15_March-PENN_DM_RL.pdf", `PA-WPP2` = "PA24.01.15_March-WPP_DM_RL.pdf"
        )
        
        expect_equal(
          actual,
          expected
        )
      }
    )
    #> ── Failure: files are named correctly ──────────────────────────────────────────
    #> `actual` (`actual`) not equal to `expected` (`expected`).
    #> 
    #>      names(actual) | names(expected)               
    #>  [3] "MD-OTH1"     | "MD-OTH1"       [3]           
    #>  [4] "MD1"         | "MD1"           [4]           
    #>  [5] "NJ1"         | "NJ1"           [5]           
    #>  [6] "PA1"         - "OH1"           [6]           
    #>  [7] "AMEREN2"     - "PA1"           [7]           
    #>  [8] "COMED2"      - "AMEREN2"       [8]           
    #>  [9] "PA-DUQ2"     - "COMED2"        [9]           
    #> [10] "PA-METED2"   - "PA-DUQ2"       [10]          
    #> [11] "PA-PECO2"    - "PA-METED2"     [11]          
    #> [12] "PA-PENELEC2" - "PA-PECO2"      [12]          
    #>  ... ...             ...             and 4 more ...
    #> 
    #> actual[3:14] vs expected[3:16]
    #>   MD-OTH1"MD-OTH24.01.15_March_DM_RL.pdf"
    #>   MD1"MD24.01.15_March_DM_RL.pdf"
    #>   NJ1"NJ24.01.15_March_DM_RL.pdf"
    #> - PA1"PA24.01.15_March_DM_RL.pdf"
    #> + OH1"OH24.01.15_March_DM_RL.pdf"
    #> - AMEREN2"AMEREN24.01.15_March-2_DM_RL.pdf"
    #> + PA1"PA24.01.15_March_DM_RL.pdf"
    #> - COMED2"COMED24.01.15_March-2_DM_RL.pdf"
    #> + AMEREN2"AMEREN24.01.15_March-2_DM_RL.pdf"
    #> - PA-DUQ2"PA24.01.15_March-DUQ_DM_RL.pdf"
    #> + COMED2"COMED24.01.15_March-2_DM_RL.pdf"
    #> - PA-METED2"PA24.01.15_March-METED_DM_RL.pdf"
    #> + PA-DUQ2"PA24.01.15_March-DUQ_DM_RL.pdf"
    #> - PA-PECO2"PA24.01.15_March-PECO_DM_RL.pdf"
    #> + PA-METED2"PA24.01.15_March-METED_DM_RL.pdf"
    #> - PA-PENELEC2"PA24.01.15_March-PENELEC_DM_RL.pdf"
    #> + PA-PECO2"PA24.01.15_March-PECO_DM_RL.pdf"
    #> and 4 more ...
    #> Error:
    #> ! Test failed
    #> Backtrace:
    #>      ▆
    #>   1. └─testthat::test_that(...)
    #>   2.   └─withr (local) `<fn>`(`<env>`)
    #>   3.     ├─base::tryCatch(...)
    #>   4.     │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
    #>   5.     │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
    #>   6.     │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
    #>   7.     └─base::eval(handler$expr, handler$envir)
    #>   8.       └─base::eval(handler$expr, handler$envir)
    #>   9.         └─reporter$stop_if_needed()
    #>  10.           └─rlang::abort("Test failed", call = NULL)
    

    Created on 2023-12-26 with reprex v2.0.2