Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if tests are coupled #1938

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions .dev/run-test-files-in-random-order.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
library(cli)
library(glue)
withr::local_envvar(TESTTHAT_PARALLEL = "FALSE")
pkgload::load_all(".")

test_script_paths <- testthat::find_test_scripts("tests/testthat")
seed <- sample.int(1e6, 1L)
cli_inform("Chosen seed for the current test run: {seed}")
set.seed(seed)
randomized_test_script_paths <- sample(test_script_paths)

any_test_failures <- FALSE
any_test_errors <- FALSE

test_path <- function(path) {
report <- as.data.frame(testthat::test_file(path, reporter = "silent"))
has_test_failures <- any(report$failed == 1L)
has_test_errors <- any(report$error == 1L)
if (has_test_failures) {
cli_alert_danger(glue("Tests in `{path}` are failing."))
any_test_failures <<- TRUE
failed_tests <- tibble::as_tibble(subset(report, failed == 1L)[, c("test", "result")])
print(glue::glue_data(failed_tests, "Test `{test}` is failing:\n{purrr::pluck(result, 1L, 1L)}"))
}
if (has_test_errors) {
cli_alert_danger(glue("There was error while running tests in `{path}`."))
any_test_errors <<- TRUE
errored_tests <- tibble::as_tibble(subset(report, error == 1L)[, c("test", "result")])
print(glue::glue_data(errored_tests, "Test `{test}` has error:\n{purrr::pluck(result, 1L, 1L)}"))
}
if (!has_test_failures && !has_test_errors) {
cli_alert_success(glue("All tests passing in `{path}`."))
}
}

cli_rule()
cli_inform("Running tests in random order:")
cli_rule()

purrr::walk(randomized_test_script_paths, test_path)

cli_rule()
if (any_test_failures) {
cli_abort("Tests in some files are failing.")
}

if (any_test_errors) {
cli_abort("There was error while running tests in some files.")
}

if (!any_test_failures && !any_test_errors) {
cli_alert_success("Tests from all files are passing!")
}
cli_rule()
35 changes: 35 additions & 0 deletions .github/workflows/check-random-test-order.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Run tests in random order
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: check-random-test-order

jobs:
check-random-test-order:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v3

- uses: r-lib/actions/setup-r@v2
with:
r-version: "release"
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
pak-version: devel
extra-packages: |
local::.

- name: Run Tests in Random Order
run: |
options(crayon.enabled = TRUE)
callr::rscript(".dev/run-test-files-in-random-order.R")

shell: Rscript {0}
Loading