Search code examples
unit-testingmsbuildnunitteamcitymsdeploy

How can I run unit tests and deploy my code without building the solution twice?


I am setting up a TeamCity deployment for an ASP.NET site that has unit tests using NUnit. How can I set up my build in a way that only requires me to build once but allows me to run my tests before deploying my code?

My build configuration is currently set up like this:

1) MSBuild - Build with no deploy.

2) NUnit - Run unit tests.

3) MSBuild - Build again, adding /p:PublishProfile and /p:DeployOnBuild parameters to deploy after building via WebDeploy.

Ideally I'd like to only build once to shorten the length of the build but I don't want to run the unit tests after the code has already been deployed.

Can I invoke NUnit in the middle of an msbuild step? That would allow me to merge my 3 steps in to 1.

Alternatively is there a way I can use the msdeploy command in TeamCity? That could replace my third step, as long as I can still hook that up to my WebDeploy endpoint.


Solution

  • This is simple: Just write your own msbuild file to run three targets, each depending the previous one:

    Target Build - builds the project (i.e. solution file) Target RunUnitTests - (depends on Target Build). This runs the unit tests. Target Deploy - (depends on Target RunUnitTests). This deploys your application to where ever.

    It would look like this:

    <Target Name="Build">
       <!-- Do Build Stuff here -->
    </Target>
    
    <Target Name="Tests" DependsOnTargets="Build">
       <!-- Run NUnit tests here -->
    </Target>
    
    <Target Name="Deploy" DependsOnTargets="Tests">
       <!-- Deploy your stuff here -->
    </Target>
    

    Then in TeamCity, add an msbuild step and have it point to the file above. Then call the 'Deploy' target.