AbsaOSS/commons

Scalatest: Environment variable testing fixture

Closed this issue · 0 comments

wajda commented

Testing code that depends on environment variables is challenging as there is the standard Java API doesn't allow for modifying env variables at runtime. In tests however, we sometimes need to set certain environment variables before the test and clean them up after. It could be done by implementing a fixture. E.g.

class MySpec ... with EnvFixture {
  it should "set FOO variable for this test body only" in {
    setEnv("FOO", 42)
    // execute some test code that reads FOO environment variable
    // via the standard Java API like System.getenv("FOO")
  }
}