How to ensure english error messages in testthat unit tests

与世无争的帅哥 提交于 2019-12-11 02:13:07

问题


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.

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)

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

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?

The R console is in German language, how can I set R to English?

Edit 3: As a first work-around I have put

Sys.setenv("LANGUAGE"="EN")  # work-around to always create english R (error) messages

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...


回答1:


Setting Sys.setenv("LANGUAGE" = "EN") works for me as well.

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 sets environment variables (for other processes called from within R or future calls to Sys.getenv from this R process).

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 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")


来源:https://stackoverflow.com/questions/47977951/how-to-ensure-english-error-messages-in-testthat-unit-tests

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!