gpt4 book ai didi

使用没有包的 testthat 运行单元测试

转载 作者:行者123 更新时间:2023-11-28 19:43:45 25 4
gpt4 key购买 nike

我有一个 Shiny 的应用程序,它使用了 4 个函数。我想测试这些功能,但它不是一个包。我应该如何构建我的代码?并在没有开发工具的情况下执行这些测试?

最佳答案

您可以使用 testthat::test_dir()testthat::test_file() 执行测试。既不依赖于包中的代码,也不依赖于使用 devtools,只是 testthat包。

关于如何构建代码的要求很少。如果是我,我会创建一个 tests 目录并在其中添加我的测试脚本,它看起来像这样:

|- my_shiny_app
| |- app.R
| |- tests
| |- test_foo.R
| |- test_bar.R

然后您可以使用 test_dir('tests') 运行您的测试,假设您在 my_shiny_app 目录中。

您的测试脚本将具有相同的 structure they have for packages但是您需要将 library() 调用替换为 source() 引用定义函数的文件。

关于使用没有包的 testthat 运行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45587660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com