I need to write a unit test for some C++ code that checks for the presence of an environmental variable. I'm using MSVS 2008 and gtest as my framework. I add the environmental variable using putenv, I check the environmental variable using getevn, but I can't figure out how to remove it so that no other test will see it. I realize this is probably easy, but I can't find the answer on the internet. Thanks
Delete Environment Variables You need to just use the unset command with the variable name to delete it. This command will remove the variable permanently.
Using the env Command Here, we're using the -i flag, which clears the variables exported by the user and starts a new session of Bash with default values.
Now that you have set many environment variables on your system, you may want to unset some of them if you don't use them anymore. On Linux, there are two ways of unsetting environment variables : by using the unset command or by deleting variable entries into your system files.
Calling putenv
again specifying "SOME_VAR="
as parameter will delete environment variable SOME_VAR
. btw, Microsoft recommends using _putenv
as putenv
is deprecated.
You could always fork/exec a subprocess to do just the putenv
/getenv
testing, and then when it terminates there isn't any stray environment left around.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With