I wanted one of my coded UI tests to work in different environments, and to do that I wanted to override one of the URLs. I wanted to set that up in a config file rather than a data source. So, I created my own configuration data class for a custom section in App.Config (MSDN Article here):
You’ll need to add a reference to the System.Configuration assembly for that to work. Adding annotations to validate the properties would also be a good idea.
Then I added the custom elements to my app.config file (which I added to my test project):
The “type” attribute is “[Configuration Class], [assembly holding your extension without the dll/exe suffix], [version], [culture], [key]”.
Next, I created a helper class to format the URL for me:
And I called the helper class to replace the recorded URL with the configured one (see lines 1 and 7 below):
There are more properties you’d want to overload, but I haven’t done that here. Vishal Joshi has a blog post that describes how to transform the app.config file for different build configurations, and that’s something else I’d like to do.
Some housekeeping is probably a good idea, but this is just a simple example.