是否可以在 testthat 中确定测试顺序?

Is it possible to determine test order in testthat?

我正在使用 testthat 检查包裹中的代码。我的一些测试是针对基本功能的,例如构造函数和 getter。其他用于构建在基本功能之上的复杂功能。如果基本测试失败,那么预计复杂测试也会失败,因此没有必要进一步测试。

是否可以:

  1. 确保始终先完成基本测试
  2. 使测试失败停止测试过程

要回答您的问题,我认为除了对您的 test-*.R 文件进行适当的字母数字命名之外,无法确定。

来自 testthat 来源,这是 test_package 通过 test_dir 调用的函数,以获取测试:

find_test_scripts <- function(path, filter = NULL, invert = FALSE, ...) {
  files <- dir(path, "^test.*\.[rR]$", full.names = TRUE)

无论如何,让复杂的任务先失败有什么错?

要测试的最近(大概)开发是 parallel test processing

这可能不适合您的情况,因为听起来您的测试之间可能存在复杂的相互依赖关系。如果您可以隔离您的测试(无论如何我认为这将是更常见的情况),那么并行处理是一个很好的解决方案,因为它应该加快整体处理时间,并且可能会向您显示 'quick' 测试在 'slow' 测试已完成。

关于测试顺序,您可以使用 DESCRIPTION 文件中的 testthat 配置来确定测试顺序(按文件)- 正如文档所建议的那样

By default testthat starts the test files in alphabetical order. If you have a few number of test files that take longer than the rest, then this might not be the best order. Ideally the slow files would start first, as the whole test suite will take at least as much time as its slowest test file. You can change the order with the Config/testthat/start-first option in DESCRIPTION. For example testthat currently has:

Config/testthat/start-first: watcher, parallel*

Docs