【问题标题】:devtools::test() in parallel?devtools::test() 并行?
【发布时间】:2020-11-05 12:03:43
【问题描述】:

我有一个使用testthat 的测试套件,其中包含R/tests 中的多个文件,我想并行测试它们以加快测试速度。在devtools、testthat 或其他地方有没有为此实现的方法?

我尝试使用 future 包“手动”执行此操作,但 stdout 的文本呈现不可读:

# Get a vector of test files without "test-" and ".R"
test_files = list.files("tests/testthat", "test-")
test_filters = stringr::str_replace_all(test_files, c("test-|\\.R"), "")

# Run test for each file in parallel
future::plan(future::multiprocess)
future.apply::future_mapply(devtools::test, filter = test_filters)

【问题讨论】:

  • 如果您对替代框架持开放态度,tinytest 内置了并行测试运行器。很多人都在愉快地使用它,包括我自己,以及来自testthat 的一些人。我可以扩展,但也许你想留在testthat ...
  • @DirkEddelbuettel, tinytest 看起来很有前途,我可能想要过渡(还有其他原因)。所以我很感激tinytest 的回答。

标签: r parallel-processing devtools testthat


【解决方案1】:

RUnit 的长期用户最近切换到tinytest 的警告,您正在寻找的功能已经存在于tinytest 中。我认为有人已经或可能在某个时候为testthat 构建了一个并行测试运行程序,但在“此时此地”中,我们确实有tinytest 具有非常好的行为、良好的文档和从RUnit 转换的线索或testthat。

我最喜欢tinytest 的功能是在包中默认安装测试、缺少其他依赖项和并行运行器。

还有一个警告:我更喜欢这种方式的命令行,而不是 R 提示符,因为可能总会有某种形式的副作用。所以我在littler 中添加了一个小测试运行器wrappre tt.r:

edd@rob:~$ tt.r -h
Usage: tt.r [-h] [-x] [-a] [-b] [-d] [-f] [-n NCPUS] [-p] [-s] [-z] [ARG...]

-a --all            use test_all mode [default: FALSE]
-b --build          use build-install-test mode [default: FALSE]
-d --directory      use directory mode [default: FALSE]
-f --file           use file mode [default: FALSE]
-n --ncpus NCPUS    use 'ncpus' in parallel [default: getOption]
-p --package        use package mode [default: FALSE]
-s --silent         use silent and do not print result [default: FALSE]
-z --effects        suppress side effects [default: FALSE]
-h --help           show this help text
-x --usage          show help and short example usage 
edd@rob:~$ 

(我应该在这里补充一点,感谢docopt,编写这样的包装器很容易。)

然后我们就这样做

edd@rob:~$ tt.r -n 4 -p anytime
starting worker pid=642068 on localhost:11092 at 17:11:25.636
starting worker pid=642067 on localhost:11092 at 17:11:25.654
starting worker pid=642065 on localhost:11092 at 17:11:25.687
starting worker pid=642066 on localhost:11092 at 17:11:25.689
Running test_gh_issue_12.R............    2 tests OK 
Running test_gh_issue_56.R............    7 tests OK 
Running test_gh_issue_33.R............    2 tests OK 

Running test_all_formats.R............    0 tests    ris or Windows or Release
Running test_assertions.R.............    2 tests OK 
Running test_calc_unique.R............    4 tests OK 
Running test_gh_issue_100.R...........    2 tests OK 
Running test_simple.R.................   34 tests OK 
Running test_utilities.R..............    2 tests OK 
Running test_bulk.R................... 2328 tests OK 
[1] "All ok, 2383 results"
edd@rob:~$ 

你看到有一点输出被吞没了。

您当然也可以从 R 手动运行它:

R> tinytest::test_package("anytime", ncpu=4)
starting worker pid=651865 on localhost:11762 at 17:14:45.970
starting worker pid=651864 on localhost:11762 at 17:14:45.980
starting worker pid=651863 on localhost:11762 at 17:14:45.980
starting worker pid=651862 on localhost:11762 at 17:14:45.984
Running test_gh_issue_12.R............    2 tests
 Exited 'test_all_formats.R' at line 24. Skipping Solaris or Windows or ReleaseOK 
Running test_all_formats.R............    0 tests    
Running test_gh_issue_56.R............    7 tests OK 
Running test_assertions.R.............    2 tests OK 
Running test_gh_issue_33.R............    2 tests OK 
Running test_calc_unique.R............    4 tests OK 
Running test_gh_issue_100.R...........    2 tests OK 
Running test_simple.R.................   34 tests OK 
Running test_utilities.R..............    2 tests OK 
Running test_bulk.R................... 2328 tests OK 
[1] "All ok, 2383 results"
R> 

文件、目录、构建+安装+测试循环等还有其他运行程序。嘿,如果在这一切之后你仍然不喜欢它,马克会把你的钱还给你:)

PS 在这里,例如在Rcpp 中,我有一些测试“变暗”,因为它们会产生大量的 cmdline 噪音,因此只有在设置了 opt-in var 时才会在包测试中发生。因此,上面的几个“零测试运行”。这是我的设置,不是tinytest 问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-01
    • 2020-10-13
    • 2018-03-02
    • 2017-05-01
    • 2018-06-22
    • 2020-02-18
    • 2017-06-10
    • 2014-03-07
    相关资源
    最近更新 更多