Monday, May 6, 2013

How to Automate Unit Testing with TeamCity (Part 2)

Please notice that this is a multi-part, step-by-step guide, on how to automate builds in TeamCity CI Server. You can find part 1 of this series here.

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

The next thing we need to do is to setup a TeamCity project for our new MVC application. In this case, we want to setup a new TeamCity project for this second MVC application sample (we created a previous MVC application in the previous article series).

You can follow the steps that I explained in my previous post. Alternatively, you can just add the unit test project to the existing MVC app we created before, and update TeamCity from here.

To create a new Build configuration, follow the steps:

  1. Login into TeamCity admin page
  2. Click on the Administration link on the top right link, next to the search box.
  3. Your projects and associated build configurations will be displayed here.

    TC_build_web_57
  4. Click on the “Create build configuration” link, below the project we created before (“MVCApplication”).

    TC_build_web_58
  5. Enter the general details for the new Build as we did before.

    TC_build_web_59
  6. Configure the version control settings. Notice that now you can attach the new build to the existing VCS root we created before. You can leave the other fields with their default settings.
  7. Click on the “Add Build Step” button to add the build steps.

    TC_build_web_60
  8. As we did before, we will use MSBuild as our build mechanism. This will enable TeamCity to build our project as if we were building it in Visual Studio.

    TC_build_web_62
  9. Fill in the steps to build the solution as we did before. Notice that you need to enter the correct relative path to the MSBuild script. If you created a new MVC solution as I did, you can copy the same MSBuild script we created before to the new solution (create a new folder named Build, and add the existing script). The script is generic, so it will work for this project without issues.

TC_build_web_61

Now that we have the Build step configured and the MSBuild script in place, let’s finish the rest of the steps of the build configuration in TeamCity.

TC_build_web_63

  1. Add the build trigger, so the build step is executed when changes are committed.

    TC_build_web_65
  2. We don’t have dependencies to add, so jump to the build parameters. Again we need to add SolutionDir, SolutionFile, ProjectName, Configuration and BuildFilePath parameters.
  3. Nothing to do for the last step, Agent Requirements so we are ready.

To test our work so far, we need to commit our local changes with the new (or updated) solution and verify that TeamCity is able to pick the changes and execute the Build successfully. You could just run the build manually from the TeamCity admin panel, but if you haven’t done your initial commit, there’s nothing for TeamCity to build.

To commit your changes in SVN:

TC_build_web_66
  1. Right click on the solution root folder and Add the files to source control.

    TC_build_web_69

    TC_build_web_70
  2. Remove the bin and obj folders from source control by adding them to the ignore list (for both the web project and the Test project)

    TC_build_web_71
    TC_build_web_72

    TC_build_web_73

  3. Commit the changes to the SVN server
  4. Verify the build execution in TeamCity

TC_build_web_78

TC_build_web_80

Notice that since both projects are attached to the same VCS root, both build projects will run when changes are committed to the repo.

TC_build_web_82

You can always add rules to the VCS triggering to avoid compiling unwanted projects when committing changes to the repo. Because of this, the build might take some more time, since it will need to perform a new checkout of the new changes, containing the new app.

TC_build_web_81

You need to make sure the the MSTest binaries are available on the server. If you have Visual Studio and the .NET Framework (the versions you are targeting with your app) installed on the server, everything should work. If you have issues building the test project or want to know how to have MSTest working on the server without Visual Studio, check my post about the subject.

Next Article: Part 3: Configuring Automated Testing

No comments:

Post a Comment