'The TestContext.TestName property NEVER changes

GOAL:

I want to use the TestContext.TestName property to extract the name of the test being ran so that my [TestCleanup] function can log the outcome to our bespoke results repository, automatically when every test completes.

PROBLEM:

Even in my basic 'sanity check' test project that contains 5 tests that are similar to the structure below:

[TestMethod]
public void TestMethodX()
{
    Console.WriteLine(String.Format("In Test '{0}'",_ctx.TestName));
    Assert.IsTrue(true);
}

With a Class 'initializer' like below which sets _ctx for me:

[ClassInitialize]
public static void ClassInit(TestContext Context)
{
    _ctx = Context;
    Console.WriteLine("In ClassInit()");
}

[[NOTE: the Console.WriteLines are purely there for me to hover the mouse over and inspect value/properties, etc.]]

The _ctx.TestName NEVER changes from the name of the first test in the run of tests, i.e. If I was to run all five tests ('TestMethod1', 'TestMethod2', 'TestMethod3', etc.) they all log 'TestMethod1' as their testname in my results repository. Running the tests individually it works fine, but that is of no use to me as I need to be able to run 10's/100's/1000's of tests against my application and have the testContext handle the testname or me.

I have tried this several times now and searched the internet loads and haven't anyone else with this problem, so i'm either: unique with this problem, have poor 'Google-Fu' skills, or am doing something REAL stupid. Hopefully this makes sense and someone has the answer.

Thanks in advance,

Andy



Solution 1:[1]

MSTest.exe outputs can be configured to output a .trx (xml)file with the complete results of you test, names , passed or failed and output from any of those test, there is also a tool to convert the TRX file to HTML http://trxtohtml.codeplex.com/

Hope this helps

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Iain