1

I am developing a package and all of the 57 tests in the package pass when I use devtools::test(), but there is 1 test that does not pass when I use devtools::check().

-- Failure (test-plot_admissions_discharges_day_of_week.R:53:3): Admission numbers by day of the Week for improvised data --
`result` not equal to `correct_answers`.
Component "Value": Mean absolute difference: 0.7941176

[ FAIL 1 | WARN 4 | SKIP 0 | PASS 56 ]
Error: Test failures
Execution halted

Here is the test file:

test_that("Admission numbers by day of the Week for improvised data", {

  # Specify correct results
  correct_answers <- tibble::tibble(
    Weekday = as.character(c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")),
    Event = c(
      "Avg_admissions", "Avg_admissions", "Avg_admissions", "Avg_admissions", "Avg_admissions", "Avg_admissions", "Avg_admissions",
      "Avg_discharges", "Avg_discharges", "Avg_discharges", "Avg_discharges", "Avg_discharges", "Avg_discharges", "Avg_discharges",
      "Non_emergency_admissions", "Non_emergency_admissions", "Non_emergency_admissions", "Non_emergency_admissions", "Non_emergency_admissions",
      "Non_emergency_admissions", "Non_emergency_admissions"
    ),
    Value = c(0.0, 0.5, 0.5, 0.5, 0.5, 0.5, 1.0, 2.0, 0.0, 1.5, 1.0, 1.0, 1.0, 1.0, 0.0, 0.5, 0.5, 0.5, 0.5, 0.0, 0.5)
  )

  correct_answers <- correct_answers %>%
    dplyr::arrange(Weekday, Event)

  # Creating input data
  adm <- c("2018-12-09 09:30:00", "2018-12-09 09:30:00", "2018-12-09 09:30:00", "2018-12-11 09:30:00", "2018-12-09 09:30:00", "2018-12-12 09:30:00", "2018-12-09 09:30:00", "2018-12-13 08:00:00", "2018-12-09 09:30:00", "2018-12-14 08:00:00", "2018-12-09 09:30:00", "2018-12-09 09:30:00", "2018-12-15 08:00:00", "2018-12-16 08:00:00", "2018-12-16 08:00:00")
  disch <- c("2018-12-12 09:30:00", "2018-12-10 09:30:00", "2018-12-12 09:30:00", "2018-12-12 09:30:00", "2018-12-13 09:30:00", "2018-12-13 09:30:00", "2018-12-14 08:00:00", "2018-12-14 08:00:00", "2018-12-15 08:00:00", "2018-12-15 08:00:00", "2018-12-16 09:30:00", "2018-12-16 09:30:00", "2018-12-17 08:00:00", "2018-12-17 08:00:00", "2018-12-17 09:00:00")
  spell_class_col <- c(
    "ed_admission", "direct_admission", "ed_admission", "direct_admission", "ed_admission",
    "direct_comp_admission", "ed_admission", "direct_admission", "ed_admission", "direct_admission",
    "ed_admission", "direct_comp_admission", "ed_admission", "ed_admission", "direct_admission"
  )
  spell_start <- as.POSIXct(adm, tz = "Europe/London")
  spell_end <- as.POSIXct(disch, tz = "Europe/London")

  test_avg_adm_disch <- tibble::tibble(
    spell_number = 101:115,
    spell_start,
    spell_end, spell_class_col
  )

  # Run admissions&discharges graph
  result <- plot_admissions_discharges_day_of_week(
    data = test_avg_adm_disch,
    startDate = as.Date("2018-12-10", tz = "Europe/London"),
    endDate = as.Date("2018-12-23", tz = "Europe/London"),
    returnPlot = FALSE,
    hospitalName = "{hospital_name}"
  )

  result$Weekday <- as.character(result$Weekday)

  result <- result %>%
    dplyr::arrange(Weekday, Event)

  # Test results are correct
  expect_equal(tibble::as_tibble(result), tibble::as_tibble(correct_answers), tolerance = 0.01)
})

result​ and correct_answers​ are dataframes that are being compared at the end of the test with expect_equal​. And they are indeed identical when I execute the code inside test file line by line and call identical()​ on the dataframes. Value​ is the column that both dataframes have, and for some reason this is where mismatch happens.

This is what the result and correct_answers dataframes look like when I execute the code manually (as I mentioned previously, they are identical).

enter image description here

As mentioned in Hadley's book, it most likely has something to do with testing environments:

Occasionally you may have a problem where the tests pass when run interactively with devtools::test(), but fail when in R CMD check. This usually indicates that you’ve made a faulty assumption about the testing environment, and it’s often hard to figure it out.

Could someone please help me debug this issue? I checked the following resources but found no luck:

Thanks!

I am using R 4.1.0, Windows OS. Here is the DESCRIPTION FILE with the dependencies:

Depends: 
    R (>= 2.10)
Imports: 
    DescTools (>= 0.99.40),
    dplyr (>= 1.0.4),
    forcats (>= 0.5.1),
    ggplot2 (>= 3.3.3),
    ggpubr (>= 0.4.0),
    gtools (>= 3.9.2),
    lazyeval (>= 0.2.2),
    lubridate (>= 1.7.9.2),
    magrittr (>= 2.0.1),
    naniar (>= 0.6.0),
    padr (>= 0.6.0),
    purrr (>= 0.3.4),
    qicharts2 (>= 0.7.1),
    readr (>= 1.4.0),
    reshape2 (>= 1.4.4),
    rlang (>= 0.4.10),
    scales (>= 1.1.1),
    stringr (>= 1.4.0),
    tibble (>= 3.0.6),
    tidyr (>= 1.1.2)
Suggests: 
    knitr (>= 1.31),
    rmarkdown (>= 2.12),
    testthat (>= 3.0.2),
    vdiffr (>= 0.3.3)
VignetteBuilder: 
    knitr
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.1
  • 1
    You've not shown us your test code, the code you're trying to test, your input data, your expected output nor the environments in which you're running the tests. It's going to be tricky... – Limey Jun 07 '22 at 07:40
  • Hi @Limey, I've added more information to my question. Please let me know if that helps. – Martynas Venckus Jun 07 '22 at 08:04

0 Answers0