Reputation: 499
I have an http trigger azure function and I am calling a service within that function. Within that service I am accessing environment variables using
Environment.GetEnvironmentVariable(key, EnvironmentVariableTarget.Process);
But when I debug/run my test the value returned as null. So I tried setting the Environment Variable within my TestClass as below,
Environment.SetEnvironmentVariable(key, value, EnvironmentVariableTarget.Process);
After that it is returning the value set within my TestClass. Could you please help me understand if there is any negative impact for this approach?
I am using .netCore 2.2 and Azure Functions version 2
Upvotes: 0
Views: 2178
Reputation: 5294
I don't see any negative impact on it as the scope is limited to the process only.
The **GetEnvironmentVariable(String)**
method retrieves an environment variable from the environment block of the current process only. It is equivalent to calling the GetEnvironmentVariable(String, EnvironmentVariableTarget)
method with a target value of EnvironmentVariableTarget.Process
.
Any variables added to the process block while the process is running by calling either the SetEnvironmentVariable(String, String)
method or the SetEnvironmentVariable(String, String, EnvironmentVariableTarget)
method with a target value of EnvironmentVariableTarget.Process
. These environment variables persist until the .NET application terminates.
SO if you are worrying if you test data will mess with your actual execution so it won't be the case as it will be terminated once test case execution will be done.
But in my opinion , i would maintain a separate environment variable for scalability purpose (between Actuala and for test cases ) as multiple environment e.g. dev,test ,staging might have different test value.
Additional Reference:
Hope it helps.
Upvotes: 1