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 !!