Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App.config replacements for unit tests

my continuous integration server (TeamCity) is configured to run all the unit tests in our app on build. Prior to running those tests, i need to change some of the appSettings to make them valid for our CI server. I'm achieving something similar for my web project by using the deployment project provided with Visual Studio. Can i do the same for a Test project?

Thanks, Gonzalo

like image 712
Gonzalo Avatar asked Feb 23 '11 12:02

Gonzalo


People also ask

Is app config and web config are same?

You can have a web. config for each folder under your web application. app. config is used for windows applications.

How do I run all unit tests in Visual Studio?

To run all the tests in a default group, choose the Run icon and then choose the group on the menu. Select the individual tests that you want to run, open the right-click menu for a selected test and then choose Run Selected Tests (or press Ctrl + R, T).

What can be used for unit Testing in C#?

Methods such as Assert. AreEqual , Assert. IsTrue, and others are frequently used in unit testing.


2 Answers

It's possible to use Web.config Transformations for App.config files through a workaround.

You simply have to invoke the appropriate MSBuild tasks at the right stage in your build process.
Add this code snippet to your project file:

<UsingTask
    TaskName="TransformXml"
    AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll" />

<Target Name="AfterCompile" Condition="exists('App.$(Configuration).config')">
    <!-- Generates the transformed App.config in the intermediate directory -->
    <TransformXml
        Source="App.config"
        Destination="$(IntermediateOutputPath)$(TargetFileName).config"
        Transform="App.$(Configuration).config" />
    <!-- Forces the build process to use the transformed configuration file -->
    <ItemGroup>
        <AppConfigWithTargetPath Remove="App.config"/>
        <AppConfigWithTargetPath
            Include="$(IntermediateOutputPath)$(TargetFileName).config">
            <TargetPath>$(TargetFileName).config</TargetPath>
        </AppConfigWithTargetPath>
    </ItemGroup>
</Target>

Then add additional App.config files to your project for each build configuration where you wish to apply a transformation. For example:

<ItemGroup>
    <None Include="App.config" />
    <None Include="App.Release.config">
        <DependentUpon>App.config</DependentUpon>
    </None>
</ItemGroup>

Related resources:

  • Web.config Transformation Syntax for Web Application Project Deployment
  • .Config File Transformation
like image 157
Enrico Campidoglio Avatar answered Sep 28 '22 08:09

Enrico Campidoglio


I have created a Visual Studio add in which can be used to transform app.config in the same way that web.config is transformed. You can find the add-in, SlowCheetah, at http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5.

I have posted a blog about how to get this working on a build server as well.

like image 28
Sayed Ibrahim Hashimi Avatar answered Sep 28 '22 07:09

Sayed Ibrahim Hashimi