Skip to content

Instantly share code, notes, and snippets.

@pawelpabich
Created March 4, 2012 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawelpabich/1971234 to your computer and use it in GitHub Desktop.
Save pawelpabich/1971234 to your computer and use it in GitHub Desktop.
Normalizes MbUnit test results so they can be viewed in Visual Studio
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
namespace MbUnitTestResultNormalizer
{
public class TestResultNormalizer
{
public void Normalize(string inputFolder, string outputFolder)
{
var testResults = FindAllTestResults(inputFolder);
foreach (var testResult in testResults)
{
Console.WriteLine("Processing {0} test result file", testResult);
var document = XDocument.Load(testResult);
if (!NeedsToBeNormalized(document)) continue;
NormalizeTestElements(document);
NormalizeTestResults(document);
var output = GetOutputPath(testResult, outputFolder);
document.Save(output);
Console.WriteLine("{0} test result file has been processed", testResult);
}
}
private bool NeedsToBeNormalized(XDocument document)
{
return GetGallioTestElements(document).Any();
}
private string GetOutputPath(string testResult, string outputFolder)
{
var fileInfo = new FileInfo(testResult);
return Path.Combine(outputFolder, fileInfo.Name.Replace(fileInfo.Extension, String.Empty) + "_Normalized" + fileInfo.Extension);
}
private IEnumerable<string> FindAllTestResults(string inputFolder)
{
return Directory.EnumerateFiles(inputFolder, "*.trx");
}
private void NormalizeTestResults(XDocument document)
{
var gallioTestResults = document.Descendants(XN("GallioTestResult")).ToList();
foreach (var gallioTestResult in gallioTestResults)
{
gallioTestResult.Name = XN("UnitTestResult");
gallioTestResult.SetAttributeValue("testType", "13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b");
}
}
private void NormalizeTestElements(XDocument document)
{
var gallioTestElements = GetGallioTestElements(document);
foreach (var gallioTestElement in gallioTestElements)
{
var gallioAttributes = gallioTestElement.Attributes().Where(attribute => attribute.Name.NamespaceName == "http://www.gallio.org/").
ToList();
var unitTest = new XElement(XN("UnitTest"));
unitTest.SetAttributeValue("name", gallioTestElement.Attribute("name").Value);
unitTest.SetAttributeValue("storage", gallioTestElement.Attribute("storage").Value);
unitTest.SetAttributeValue("id", gallioTestElement.Attribute("id").Value);
var testMethodNode = new XElement(XN("TestMethod"));
testMethodNode.SetAttributeValue("codeBase", gallioTestElement.Attribute("storage").Value);
testMethodNode.SetAttributeValue("adapterTypeName",
"Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapter, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
testMethodNode.SetAttributeValue("className", GetGallioAttributeValue(gallioAttributes, "TypeName") +
", " +
GetGallioAttributeValue(gallioAttributes, "AssemblyName"));
testMethodNode.SetAttributeValue("name", GetGallioAttributeValue(gallioAttributes, "MemberName"));
unitTest.Add(gallioTestElement.Descendants(XN("Execution")).Single());
unitTest.Add(testMethodNode);
gallioTestElement.Parent.Add(unitTest);
gallioTestElement.Remove();
}
}
private List<XElement> GetGallioTestElements(XDocument document)
{
return document.Descendants(XN("GallioTestElement")).ToList();
}
private string GetGallioAttributeValue(IEnumerable<XAttribute> gallioAttributes, string attributeName)
{
return gallioAttributes.Single(a => a.Name.LocalName.Contains(attributeName)).Value;
}
private XName XN(string elementName)
{
return XName.Get(elementName, "http://microsoft.com/schemas/VisualStudio/TeamTest/2010");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment