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="
https://microsoft.com/schemas/VisualStudio/TeamTest/2010" >
< 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=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</TestLinks>
< /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

  • Anonymous
    April 09, 2014
    The comment has been removed
  • Anonymous
    April 09, 2014
    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":-)
  • Anonymous
    May 23, 2014
    @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...
  • Anonymous
    February 03, 2015
    Any one can explain about highlighted things and why there are present in ordered test ?what is the use of those things?