How to create an ordered test programmatically?

Yesterday an internal customer asked me on how he can create an ordered test programmatically. This was the first time someone had asked me this but I could imagine that it is useful if you have a fixed logic to define the order and have reasonable # of ordered tests to create/maintain.

Since ordered tests is an xml file with a fixed schema, it is very easy to create it programmatically. Here is how an ordered test look like: -


<?xml version="1.0" encoding="UTF-8"?>
< OrderedTest name="orderedtest1" storage="c:\users\aseemb\desktop\unittestproject1\unittestproject1\orderedtest1.orderedtest" id="1aeba90d-9c29-4b01-ae4b-fe86bba94a82" xmlns="">
  < TestLinks>
    < TestLink id="5b688acc-bf10-4c7e-4b60-69d8596bdd90" name="TestMethod1" storage="bin\debug\unittestproject1.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
< /OrderedTest>

So to create an ordered test, you have to generate an xml similar to this. I am assuming that most of the xml is self explanatory (xsd is in %VS Install dir%\Xml\Schemas\vstst.xsd ) and the only confusing thing is the highlighted guids. But in case you have any question on the xml, please feel free to leave a comment and I will revert back.

Here the first guid (1aeba90d-9c29-4b01-ae4b-fe86bba94a82) is the ID of ordered test and for this you can generate a new guid and put it there.

The second guid is about the ID of the testMethod1 and you can use a function similar to the one mentioned below to convert the test (<Name space name>.<class name>.<test method name>) to a guid.

private static Guid GuidFromString(string data)

SHA1CryptoServiceProvider provider = new SHA1CryptoServiceProvider();

byte[] hash = provider.ComputeHash(System.Text.Encoding.Unicode.GetBytes(data));

byte[] toGuid = new byte[16];
  Array.Copy(hash, toGuid, 16);

return new Guid(toGuid);


Enjoy !!

Comments (4)

  1. Carter says:

    Thanks for the post, this is useful. I notice that when I change the namespace of tests, that it will break my *.orderedtest files. I suspect this is because VS internally uses a method similar to what you have above, and computes a hash of the "namespace.class.method".  This can be quite an inconvenience when changing namespace or class names across a large number of tests. (Think several hundred)

    Do you know of a clean and easy way to update test guids when performing such refactors? At the moment, I'm stuck with quite a few broken *.orderedtest files because of a simple namespace change.

    (This would have been much easier, and would be just a simple text replace, if the *.orderedtest just used the "namespace.class.method" text directly, instead of using this brittle hash approach…)


  2. Carter says:

    Sorry, I meant to say…  "do you know of a clean and easy way to do this, other than to begin programmatically generating my *.orderedtest files"


  3. Patryk says:

    @Carter : The guy who set this up is a fking moron! How the fk you want to work with that if you can't rename your DLL, namespace and/or class. I'm flabbergasted. I feel like killing someone…

  4. Ratna says:

    Any one can explain about highlighted things and why there are present in ordered test ?what is the use of those things?

Skip to main content