Monday, May 6, 2013

How to Automate Unit Testing with TeamCity (Part 1)

Please notice that this is a multi-part, step-by-step guide, on how to automate builds in TeamCity CI Server.

Part 1: Creating the Sample Project (including unit test project)

First of all, we need to create our test solution, which will include our sample ASP.NET MVC 4.0 project and our Unit Test project. Let’s get rolling.


  1. Create a new MVC Project in Visual Studio.
  2. Make sure the target Framework is supported by your TeamCity CI Server version (in this case .NET Framework 4.0)

  3. Create a Unit Test project along with it when creating the project in Visual Studio
  4. Once the application is created, compile it and run it to make sure everything works correctly.

  5. Do a few basic modifications on the Home controller to have your own personal messages displaying on the page. In my case I’m just changing the ViewBag.Message properties for the Index, About and Contact actions on the HomeController.cs class.


Since we just created a unit test project, we are now able to run the tests for our MVC application. Let’s do it right away.


Click on TEST/Run/All Tests. This will run all the tests in your project. You will be able to see the tests running, and you will get a failure.

TC_build_web_53  TC_build_web_54   

The reason is the default unit test is comparing the default ViewBag.Message that you just changed. Lets change the Unit Test message to match the new one. After changing the message for the correct one, and running the tests again, you should get a success message for the unit tests that ran.

TC_build_web_55  TC_build_web_56

This is a fairly trivial setup, which doesn’t really prove much, but the whole point is setting up TeamCity to build, test and deploy our project on our server environment, so let’s leave it like that for now.

Next Article: Part 2: Creating the TeamCity Build (building the unit test project)

1 comment: