How to Diagnose a Test Failure on TeamCity

Have you ever had that unit test that works locally, but fails on team city. Turns out you can debug them and find out that it isn’t team city’s fault.

Failing Team City Builds

You run the test locally, pass, run them on team city, fail. Alright, time to get serious, 1, pull the TeamCity artifact down locally, 2, run MSTest from command line like TeamCity does, 3, start debugging. Most unit test frameworks throw exceptions when a test fails, great way to debug into the test.

Make sure you have an artifact from TeamCity or you can’t pull down the test. Well, unless you are an admin on the box.

TeamCity Test Artifact

TeamCity Download Artifact

Visual Studio Attach to Process

Open up Visual Studio Command Prompt for VS2015.

[code]
mstest /testcontainer:MyTests.dll
[/code]

Next, attach to the MSTest process QUICKLY. MSTest launches QTAgent32.exe for running the actual unit tests but it won’t stop to wait for you.

Attach To Process QTAgent32.exe

In the end, it turned out another test ran first, and our second tests couldn’t set a private static readonly because of its if null check.

[code langauge=”csharp”]
private static readonly List<int> Codes = new List<int>();
[/code]

2 thoughts on “How to Diagnose a Test Failure on TeamCity

  1. Thank you for useful post, Josh!
    I’ve solved my issue with Teamcity-failed tests even without debugging – static collection mention sets me on the right track 🙂
    I also have a question: have you considered usage of System.Diagnostic.Debugger.Break() method in test code to avoid necessity of the quick attaching?

    1. Rus, yes I have, I just have a bad habit of leaving that in and forgetting about it. Most of the code I work on pulls from the source control provider, so I’m afraid to check in code like that.

Leave a Reply

Your email address will not be published. Required fields are marked *