Fixing bugs by writing tests (write and run test)
Second post in series.
2. Write and run a test that should fail
Now that I have found the bug, I will write a unit test that fails when that bug is found, using the testthat
R package.
The Plan
The bug arises from the file names in the SuSiEx output. Sometimes Nextflow passes ancestry labels (e.g., SAS, EUR, AFR) in different orders when running SuSiEx. My package assumes that all output files have the same ancestry order. If they don’t, the function isn’t meant to run, it should error out to alert the user that the ancestries in the input files are not ordered correctly.
To catch this issue, I created test data by copying actual SuSiEx output files that have file names with different orders of ancestry labels, and then clearing their contents (since only the file names matter). The test inspects the file names to ensure the ancestry labels appear in a consistent order across all files. If they don’t, it triggers an error, letting the user know their input files are wrong. The test will expect this error.
Write test: test-format_results.R
test_that("format_results throws error if file names have more than one ancestry order", {
# Get the path to where the test data is stored
# The file names in test data directory do not all have the same order of ancestry labels
source_path <- system.file("extdata", "ancestry-labels-not-ordered", package = "susiexR")
# Define ancestries used in the test data
ancestries <- c("SAS", "EUR", "AFR")
expect_error(format_results(source_path, ancestries = ancestries),
"Error: The file names do not have the same order of ancestry labels or the ancestries are not immediately separated by hyphens, underscores, or periods.")
})
For help with best practices on writing tests and to see some good examples of writing tests. I refered to the “Testing” Chapter in this book: R Packages (2e) Learn how to create a package, the fundamental unit of shareable, reusable, and reproducible R code. (Hadley Wickham and Jennifer Bryan). I also took a look at some of the R tidyverse
packages on GitHub, looking at how they structure their packages, write functions and the contents of their unit tests.
Run test:
Click on “Run Tests” in R Studio (or use function test_file()
). Expected output is that this test fails, which is what we see here:
Next steps
- Add this logic to the function
format_results()
. - Re-run test, it should now pass.
- Refactor code.