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:
<appsettings> <add key="BaseUri" value="http://127.0.0.1/" /> <!--<add key="BaseUri" value="http://dev1Environment/"/> <add key="BaseUri" value="http://dev2Environment/"/>--> </appsettings>
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.
1. Open Visual Studio 2. Go to Tool => Extensions and Updated 3. Search Online for SlowCheetah
Within your app.config add an environment value to a config section or appSettings for example:
<appsettings> <add key="BaseUri" value="http://127.0.0.1/" /> </appsettings>
Lets say you are working with a couple of environments
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.
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.
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).
<appsettings> <add key="BaseUri" value="http://myTestServer/" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" /> </appsettings>
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.
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.