Bertrand Pons
Bertrand Pons

Reputation: 259

Recommendations on runsettings file for connection string override

I have several tests projects in C# that use Database to perform tests. I'd like to create a runsettings file in order to override connection string to this DB at test execution time when using Azure DevOps.

I have not succeeded in writing the runsettings file properly yet.

Do you have any tips or recommendations on how to do that?

Upvotes: 2

Views: 873

Answers (1)

Felipe Oriani
Felipe Oriani

Reputation: 38598

You could transform your configuration file. For sample, if you have a app.config file that store a connectionstring to perform the tests, you could replace this connectionString with a valid database and run the tests.

There is a build step called Config Transformation and you can add it on the Azure DevOps pipeline. Actually, I use this build step when I am releasing application to different environments (so we replace the settings and connectionStrings with appropriated values). You can use it on the build pipeline.

Let's supose you have the app.config file, so, you could create an file where the variables are, for sample app.tests.config, and define it on the Config Transformation step, for sample:

enter image description here

It is based on the transformation for config files. Take a look at this like to know how to configure it:

https://learn.microsoft.com/pt-br/aspnet/web-forms/overview/deployment/visual-studio-web-deployment/web-config-transformations

I am not sure about your scenario but if you are using Unit Tests the best way to perform it is to use Mocks/Stubs (to simulate the database) instead accessing database.

Upvotes: 2

Related Questions