Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instead of "Data Row 0", "Data Row 1" etc. Output a custom name

In Visual Studio Team Foundation Server 2013, I'm using the Unit Testing Framework. Specifically, I'm using data-driven testing that will read from an XML file.

The gist of my question

Here's some sample code:

using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class DataDrivenTestingClass{
   public TestContext TestContext{get; set;}

   [TestMethod]
   [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "test.xml", "XMLTests", DataAccessMethod.Sequential)]
   public void MyOnlyDataDrivenTestFromXML(){
      var testName = TestContext.DataRow[0].ToString();
      //... blah blah blah other logic
      //<snip>
      Assert.IsTrue(true); //of course this is not what I really do
   }
}

And the output will come out like this:

Test Passed - MyOnlyDataDrivenTestFromXML (Data Row 0)
Test Passed - MyOnlyDataDrivenTestFromXML (Data Row 1)
Test Passed - ...
Test Passed - MyOnlyDataDrivenTestFromXML (Data Row N)

Clearly, (Data Row i) is not very descriptive. The XML stores the name I'd like to use instead (I could use anything else, but already having it in the xml seems convenient). I'm not sure this is possible, though.


Other information, speculation

The problem is that, while you can get the test name from TestContext via TestContext.TestName, you cannot set it. Also, you cannot (should not) simply inherit from TestContext and make one that allows you to do so.

I've also tried to set the name directly from the Properties of TestContext like so, but without success:

TestContext.Properties["TestName"] = testName;
TestContext.Properties["FullyQualifiedTestClassName"] = testName;

What's out of the question is having the tests output to a .trx file, and then post-processing that. This is because the tests are run on a build server backed by a SQL Server. There are no test logs that I will output.

If all else fails, I believe this is possible by hooking into the TFS Web Services API and writing a service that queries test results and does an edit there, but I'd rather not go this route if at all possible.

Another thing I was thinking was that it might be possible to do some kind of generic programming, templated approach that will evaluate to all the appropriate test names at compile-time, but this seems both very difficult and overkill.


Is this doable?


Related (but without answer): MSTest data driven Test set DisplayName for Rows from DataSource

like image 705
AndyG Avatar asked May 12 '14 20:05

AndyG


1 Answers

I feel like this would work better as a comment more than an answer, but alas I do not have enough reputation.

There is a similar question with an answer here: Mstest name instead Data Row with Data-Driven testing

It looks like they do TestContext.WriteLine() to get the values they want printed.

like image 86
druidicwyrm Avatar answered Oct 19 '22 05:10

druidicwyrm