2

We are using Entity Framework 6 with MS SQL server for our database integration tests (MSTests).

I am looking for a built-in way to set database connection string in my Test assembly's app.confing in the Visual Studio Team Services (VSTS) Build Definition.

Could this be done using build variables or some other neat built-in way?

other options considered (failing to find a better way):

  1. using *.runsettings to get connection details creating new Build
  2. configuration with the app.config transformation (SlowCheetah)
Ruslan
  • 9,927
  • 15
  • 55
  • 89
  • About option 2. I believe SlowCheetah is not required to run a transformation. You can edit csproj manually and use TransformXml task to make it working. Here I have found simple code sample [TransformXml Web.config while publishing](http://stackoverflow.com/questions/10316108/transformxml-web-config-while-publishing) – AndrewSilver Jun 06 '16 at 13:01

2 Answers2

2

You can use Replace Tokens task. This will allow you to replace the string in config file with the variables in build definition.

Eddie Chen - MSFT
  • 29,708
  • 2
  • 46
  • 60
1

One approach is to use tokenization

In order to implement tokenization, you can use either of these two implementations

  1. https://github.com/qetza/vsts-replacetokens-task#readme
  2. https://marketplace.visualstudio.com/items?itemName=ms-devlabs.utilitytasks

If you decide to go with the first implementation, here are steps

  1. Navigate to https://marketplace.visualstudio.com/items?itemName=qetza.replacetokens and install "Replace Tokens" into the account you have your build def in.

  2. Update the application web.config so that

    connectionString="#{MyToken}#"

  3. Add a new variable to the build definition with the name of the token. Ensure that value is PROTECTED

What will happen is that the build step will replace all the .config file tokens with the variable value specified for the build definition. Pretty neat.

Keep in mind that the integration tests would then likely FAIL for the source code repository in the state in which this task is not present. This might be annoying to some people, but is easy to get around if you use GIT and use a stash to override the token locally for integration tests.

Also, keep in mind that if you have a SQL Database(azure) with Firewall settings then the integration tests may fail due to the build agent virtual machines not having been authorized and you may need to add a new rule so that your build agent is authorized

Judy007
  • 5,484
  • 4
  • 46
  • 68