SlowCheetah, awesome tool for config transformation

Wednesday, March 5, 2014
by Sean McAlinden

SlowCheetah is an awesome Visual Studio extension for previewing and transforming xml configuration.   Whilst SlowCheetah works great in any transformation scenario, I'll be covering it's use from a test perspective.   I have quite often seen config for integration tests with multiple commented out connection strings and other settings which are  continually swapped around for pointing to different environments... for example:

    <add key="BaseUri" value="" />
    <!--<add key="BaseUri" value="http://dev1Environment/"/>
    <add key="BaseUri" value="http://dev2Environment/"/>-->

Apart from this being messy, it is also really annoying and can be avoided really easily.   At this point I should say there are many different solutions to solving this issue, the following is just one approach that I like.  

Add the SlowCheetah extension.

1. Open Visual Studio 2. Go to Tool => Extensions and Updated 3. Search Online for SlowCheetah  

App Config

Within your app.config add an environment value to a config section or appSettings for example:

    <add key="BaseUri" value="" />

Environments Setup

Lets say you are working with a couple of environments

  • Local
  • Test

Within Visual Studio select the configuration drop down,  Select Configuration Manager... from the list.   In the Configuration Manager, select the Active Solution Configuration drop down.

  In the drop down select <New...>   Name your new config "Test" and select OK, You now have a Test configuration that can be used for pointing to your test servers.   Now lets make this useful.  

Add Transform

Right click on the App.Config in your test solution, at the top of the context menu you will see: Add Transform Once selected a new config file called App.Test.config will be generated underneath your App.config.

Add Environment Config

Open up the App.Test.config. Add the following code to change the BaseUri app setting to point to the test server rather than localhost (Note the xdt:Transform and xdt:Locator attributes).

    <add key="BaseUri" value="http://myTestServer/" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />

Preview Transform

To check you transform has worked, right click the App.Test.config file and select Preview Transform from the context menu, this will open up a window showing the changes made by the transform.   

Run tests against different environments

So here is the pay off.   In the configuration drop down select the new Test configuration you created:  Your tests will now run using the App.config file with the App.Test.config transformations. So in this example, the BaseUri app setting will use the value http://myTestServer/.   These configurations can also be easily used for running the tests during automated test runs etc. so it is well worth the effort.