Skip to content

Unit tests reorg #503

@dragosmg

Description

@dragosmg

@bzkrouse as we discussed:

Problem: unit test coverage at file level is currently a bit more difficult to track. Not easy to identify if a function is tested directly and in which file.

Solution: reorganise the existing unit tests and fill in gaps (if needed).

Approach:

I propose the following steps:

  1. rebuild the 1-to-1 relationship between files under R/ and files under tests/testthat/ - basically each file under R/ to have its matching test file.
  2. check to see if any tests need to be moved around. For example, function_a() is defined in R/function_a.R, but its tests live in tests/testthat/test-function_b.R. This would require moving the test for function_a() to test-function_a.R
  3. this will allow us to identify gaps a bit easier. If function_a() could benefit from additional tests (let's say it's error behaviour is not tested) they should be added to the corresponding test file.

It might be more difficult and risky to do this in one go (one or more PRs corresponding to the above points) and definitely more tedious to review. It might be better to agree to do this in principle and tackle it in smaller chunks, as we go. e.g. if we touch a file, check to see it has tests and they are in the expected location.

Example: frmt_structure() is defined in R/frmt_plans.R. The function seems to only be indirectly tested and there is no matching test file (tests/testthat/test-frmt_plans.R does not exist).

  1. create test-frmt_plans.R
  2. move any relevant tests here (if they exist)
  3. write more tests if needed
Code used to check which `R/` files do not have matching test files
definition_files <- fs::dir_ls("R/")

build_expected_test_file_path <- function(file_name) {
  
  file_path_without_dir <- fs::path_file(file_name)
  
  fs::path(
    "tests", 
    "testthat", 
    paste0(
      "test-", 
      usethis:::compute_name(
        file_path_without_dir
      )
    )
  )
  
}

expected_test_files <- purrr::map_chr(definition_files, build_expected_test_file_path)

files_and_matching_test_file <- purrr::map_lgl(expected_test_files, fs::file_exists)

files_without_matching_test_file <- files_and_matching_test_file[files_and_matching_test_file == FALSE]

Files without a matching test- file:

  • "R/apply_col_plan.R"
  • "R/apply_col_style_plan.R"
  • "R/apply_frmt_methods.R"
  • "R/apply_table_frmt_plan.R"
  • "R/body_plan.R"
  • "R/col_style_plan.R"
  • "R/data.R" - does not need a test- file
  • "R/eval_tidyselect.R"
  • "R/frmt_plans.R"
  • "R/frmt_utils.R"
  • "R/mock_tbl.R"
  • "R/page_plan.R"
  • "R/print_to_ggplot.R"
  • "R/print_to_gt.R"
  • "R/row_group_plan.R"
  • "R/struct_utils.R"
  • "R/tfrmt.R"
  • "R/tfrmt_layer.R"
  • "R/theme_element.R"
  • "R/utils-pipe.R" - does not need a test- file
  • "R/zzz.R" - does not need a test- file

Sub-issues

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