Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In-memory integration tests and Environment Variables -- .net core

We can execute a test server for integration testing. Something like:

  • this question or the MS docs

Fine.

However, I can't pass environment variables to the test server. I can set the environment with UseEnvironment("Development") but not add variables. It should be possible, but I don't know how to do it.

Anyone Knows how this can be done?


The environment variables would carry dev logins for a 3rd party API our code base needs. For unit tests we mock this 3rd party API. A solution for my specific problem can be to mock this for integration tests as well, thus removing the need for environment variables, but even so the question remains.

like image 987
Vetras Avatar asked Feb 03 '17 17:02

Vetras


People also ask

How do I check environment variables in .NET Core?

Open project properties by right clicking on the project in the solution explorer and select Properties. This will open properties page. Click on Debug tab and you will see Environment Variables as shown below. You may change the value as per your need.

What is environment variable in .NET Core?

Environment Variables are one of the sources from which ASP.NET Core reads the configuration values. In this tutorial, let us learn how to set Environment Variables in Windows, Mac, Linux, IIS Server, Visual Studio, Visual Studio Code & dotnet run. We also learn how to read them in ASP.NET core.

What is integration test in .NET Core?

Integration tests ensure that an app's components function correctly at a level that includes the app's supporting infrastructure, such as the database, file system, and network. ASP.NET Core supports integration tests using a unit test framework with a test web host and an in-memory test server.


1 Answers

You can set an environmental variable programmatically.

System.Environment.SetEnvironmentVariable("key", "value");

MSDN writes that SetEnvironmentVariable(string, string)...

Creates, modifies, or deletes an environment variable stored in the current process... Because the environment variable is defined in the environment block of the current process only, it does not persist after the process has ended.

like image 72
Shaun Luttin Avatar answered Oct 13 '22 23:10

Shaun Luttin