best practices to set env variables #287
-
Hi,
I've read this discussion but it's not crystal clear for me if it's required to use Thanks for clarifications on that and maybe adding this to documentation could be nice :) ! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
TL;DR;It depends on how you want to handle your tests. Personal preferencePersonally, i like to follow the following rules:
But as i've said before, it really depends on what you're trying to make. DemonstrationIn the following example i have taken into consideration 4 major scopes:
Notice how we can overwrite the variable in the inner scope and the result changes accordingly (see the #!/bin/env bash
function MyMethodReadingEnvVar () {
if [[ "${MY_VAR}" ]]; then
echo "its ok. Variable is '${MY_VAR}'!";
return;
fi
echo "MY_VAR is missing in environment!";
}
# Environment variable defined at `Describe` level
Describe "ENV at 'describe' level"
MY_VAR="describe"
It "> should output its ok"
When call MyMethodReadingEnvVar
The output should equal "its ok. Variable is 'describe'!"
The status should be success
End
It "> Unsetting: should output missing env var"
unset MY_VAR
When call MyMethodReadingEnvVar
The output should equal 'MY_VAR is missing in environment!'
The status should be success
End
End
# Environment variable defined at `Test` level
Describe "ENV at 'test' level"
It "> should output its ok"
MY_VAR="test"
When call MyMethodReadingEnvVar
The output should equal "its ok. Variable is 'test'!"
The status should be success
End
It "> should output missing env var"
When call MyMethodReadingEnvVar
The output should equal 'MY_VAR is missing in environment!'
The status should be success
End
End
# Environment variable defined at `BeforeEach` level
Describe "ENV at 'hook' (BeforeEach) level"
BeforeEach 'MY_VAR="BeforeEach hook"'
It "> should output its ok"
When call MyMethodReadingEnvVar
The output should equal "its ok. Variable is 'BeforeEach hook'!"
The status should be success
End
It "> Overwriting: should output its ok"
MY_VAR="BeforeEach Overwritten"
When call MyMethodReadingEnvVar
The output should equal "its ok. Variable is 'BeforeEach Overwritten'!"
The status should be success
End
End
# Environment variable defined at `BeforeAll` level
Describe "ENV at 'hook' (BeforeEach) level"
BeforeAll 'MY_VAR="BeforeAll hook"'
It "> should output its ok"
When call MyMethodReadingEnvVar
The output should equal "its ok. Variable is 'BeforeAll hook'!"
The status should be success
End
It "> Overwriting: should output its ok"
MY_VAR="BeforeAll Overwritten"
When call MyMethodReadingEnvVar
The output should equal "its ok. Variable is 'BeforeAll Overwritten'!"
The status should be success
End
End This gives us the following output: Feel free to respond, as i'll be happy to help 😄 |
Beta Was this translation helpful? Give feedback.
TL;DR;
It depends on how you want to handle your tests.
Personal preference
Personally, i like to follow the following rules:
BeforeAll
orBeforeEach
hooks;DEBUG
to test the verbosity) i place it in theDescribe
orIt
blocks;But as i've said before, it really depends on what you're trying to make.
Demonstration
In the following example i have taken into consid…