How to ensure english error messages in testthat unit tests
Asked Answered
I

1

3

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

Industrialism answered 26/12, 2017 at 11:27 Comment(0)
N
3

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")
Nullifidian answered 20/7, 2018 at 17:46 Comment(3)
Which OS are you using?Industrialism
@RYoda That was on Windows 7. I just tried it on Linux Mint 18.2 and there this approach fails. I can set the language once using Sys.setenv("LANGUAGE" = fr_fr) but never unset it again until I relaunch the R session.Nullifidian
OK, thanks for trying, I am observing the same problems using Ubuntu 14.04 (cannot change the language reliably). BTW I like the idea of your wrapper function expect_error_lang!Industrialism

© 2022 - 2024 — McMap. All rights reserved.