如何确保单元测试中的英语错误消息 [英] How to ensure english error messages in testthat unit tests
问题描述
我有很多使用testthat
软件包的单元测试,它们会出现英语错误消息.
I have a lot of unit tests using the testthat
package that expect english error messages.
如果其他开发人员在配置为非英语语言环境的计算机上运行测试,则会以另一种语言发出错误消息,并且我的测试失败.
If other developer run the tests on a computer configured for a non-english locale the error message are emitted in a different language and my tests fail.
我如何初始化testthat以仅在测试运行时更改语言设置,而无需从R外部手动或永久更改语言或测试环境(例如此处建议的内容:
How can I initialize testthat to change the language settings only during the test run-time without manually or permanently changing the language or test environment from outside of R (like e. g. proposed here: in R how to get error messages in english)?
library(testthat)
# works only in english locales...
expect_error(log("a"), "non-numeric argument to mathematical function", fixed = TRUE)
:在运行时更改语言环境不会更改错误消息的语言(使用Ubuntu和OSX High Sierra):
Edit 1: Changing the locale during run-time does not change the language of the error messages (using Ubuntu and OSX High Sierra):
Sys.setlocale( locale = "en_US.UTF-8")
Sys.getlocale() # en_US is active now but messages are still in another language
似乎Sys.setenv("LANGUAGE"="EN")
似乎立即更改了错误消息的语言(使用OSX测试).我应该在哪里放置此命令以进行测试?在testthat.R文件中?
Edit 2: It seems that Sys.setenv("LANGUAGE"="EN")
seems to change the error message language immediately (tested using OSX). Where should I put this command for testthat? In the testthat.R file?
作为我提出的第一个解决方法
Edit 3: As a first work-around I have put
Sys.setenv("LANGUAGE"="EN") # work-around to always create english R (error) messages
进入我的testthat.R
文件在tests文件夹下(这似乎可以工作,但是我不确定这是正确还是最好的方法...
into my testthat.R
file under the tests folder (it seems to work but I am not sure whether this is the right or best way...
推荐答案
设置Sys.setenv("LANGUAGE" = "EN")
同样适用于我.
但是,当使用devtools::test()
进行测试时-就像Rstudio中的ctrl + shift + T一样-我必须在 tests/testthat/目录中的测试脚本中调用Sys.setenv()
.原因是devtools::test()
将绕过 tests/testthat.R 文件调用testthat::test_dir()
.
However, when testing with devtools::test()
- as ctrl + shift + T in Rstudio will do - I had to call Sys.setenv()
in the test scripts inside the tests/testthat/ directory. The reason being that devtools::test()
will call testthat::test_dir()
circumventing the tests/testthat.R file.
So far, this did not have undesirable side-effects. The environment variable will only be set for that particular R process as described in the help page:
Sys.setenv设置环境变量(用于从R内调用的其他进程或此R进程对Sys.getenv的将来调用).
Sys.setenv sets environment variables (for other processes called from within R or future calls to Sys.getenv from this R process).
为完整起见,您还可以在Windows上再次取消设置变量(请参见注释).
For completeness, you can also unset the variable again on Windows (see comments).
Sys.setenv("LANGUAGE" = "DE")
expect_error(log("a"), "Nicht-numerisches Argument")
Sys.setenv("LANGUAGE" = "FR")
expect_error(log("a"), "argument non numérique ")
Sys.unsetenv("LANGUAGE")
RStudio可能还会带来麻烦(我无法以交互方式更改那里的语言),但是在使用devtools::test()
执行时它可以工作.
RStudio might also give trouble (I was not able to change the language there interactively), but when executing with devtools::test()
it works.
最后,将其包装在辅助函数中.
Finally, wrapping it in a helper function.
expect_error_lang <- function(..., lang = "EN") {
Sys.setenv("LANGUAGE" = lang)
expect_error(...)
Sys.unsetenv("LANGUAGE")
}
#...
expect_error_lang(log("a"), "non-numeric")
expect_error_lang(log("a"), "Nicht-numerisches", lang = "DE")
expect_error_lang(log("a"), "argument non", lang = "FR")
这篇关于如何确保单元测试中的英语错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!