20 October 2014

I’ve been noticing (from following questions on Stackoverflow) that a lot of people are not familiar with the TeamCity’s concept of dependencies. This post will be a tutorial how to create two configurations in TeamCity, one for building a solution and another for running tests.

TeamCity supports two kinds of dependencies. With snapshot dependency we are able to create a build chain. In our case we will always want to run tests after successful build. In our configuration for running tests we will create snapshot dependency with the build configuration.

Artefact dependencies are used to transfer artefacts from one configuration to another. We’ll use artefact dependency to transfer binaries (assemblies) from Build configuration to new configuration and run tests in them.

Detailed information about dependencies can be found on TeamCity official documentation site.

Sample project can be found on bitbucket.

Once we created configuration named Build and attached VCS root, we can create build step.

In the General menu on the left pane we must also add artifacts path, in our case this is Contoso.Tests/bin/Debug => Tests.zip. This will create zip file named Tests containing binaries of our tests (and business assemblies).

For this demo purposes, we can use Visual Studio runner and build Debug configuration.

Now we can run our newly created configuration. When we check Artifacts tab in build summary we should see Tests.zip file. If we expand zip file, files with size should be shown.

With packed artifacts, we can create our second configuration.

Create new configuration named Tests. This configuration does not need any VCSs, because we will deliver needed files through the artefact dependency. First we need to add new snapshot dependency, we can do that in the dependencies menu on left pane.

For the Depend on we select our previously created build configuration (Build), we should leave Do not run new build if there is a suitable one and Only use successful builds from suitable ones checked. We want to run tests only when our build step succeed.

With the snapshot dependency created, it’s time that we create artefact dependency. In the same menu we select Add new artifact dependency and for Depend on: we select Build configuration. For the option Get artifacts from: select Build from the same chain. Build from the same chain guarantee us snapshot isolation for every build. Lets say Adam and Brian pushes code into repository at the same time. Assuming that we have triggers configured; TeamCity will create snapshot isolation for Adam running Build step and creating artefacts with only Adam’s changes, those artefacts will be passed on into Tests configuration where are will be run. Same thing happens with Brian’s push, but since Brian did push 2 seconds later his snapshot will contain Adams changes too. In case of failed test in Adam’s changes only Adam will receive build failure notification. I will discuss more about triggering and notifications for TeamCity’s configurations in some other post.

In Rules we define how we want our artifacts extracted. For this demo we will just dump them into root directory. So rule should be Tests.zip!** => .

Last thing we need to create is to add a build step. Add MSTest runner and for assembly path write Contoso.Tests.dll. Previously defined snapshot dependency will put the file into a working root folder of this build.

When we run the configuration, tests are being executed. You can also notice that Build configuration was not triggered. Since there is already build solution and there are no pending changes, snapshot dependency will not start first Build configuration only second.



blog comments powered by Disqus