How to register a unit test record and leave it in MSTest

I am using MSTest and I want to write a log entry before and after the test finishes. Obviously, I don’t want to add custom logging code at the beginning and at the end of each test - this would only make the test unreadable and seem like a lot of effort (I have> 500 tests)

Using TestInitialize and TestCleanup seemed like a way, but I can't get the name of the test.

Does anyone know how to do this?

+3
logging mstest
source share
3 answers

In MSTest, the name of the test case is available in the validation context property . Thus, to access it in the test initialization method (as well as the test), you can use something like this: -

[TestInitialize()] public void MyTestInitialize() { if (string.Equals(**TestContext.TestName**, "TestMethod1", StringComparison.OrdinalIgnoreCase)) { } } 

Relations Aseem Bansal

+3
source share

We use nLog for logging and the base class to test initialization and check for cleanup, which is logged if the test passes or not.

This is the base class:

 using Microsoft.VisualStudio.TestTools.UnitTesting; using NLog; namespace Tests { public class TestBase { private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); public TestContext TestContext { get; set; } [TestCleanup] public void TestCleanup() { string testName = string.Format("{0}.{1}", TestContext.FullyQualifiedTestClassName, TestContext.TestName); UnitTestOutcome currentTestOutcome = TestContext.CurrentTestOutcome; string message = string.Format("Test '{0}' {1}", testName, currentTestOutcome.ToString().ToUpperInvariant()); if (currentTestOutcome != UnitTestOutcome.Passed) { Logger.Error(message); } else { Logger.Info(message); } } [TestInitialize] public void TestInitialize() { string testName = string.Format("{0}.{1}", TestContext.FullyQualifiedTestClassName, TestContext.TestName); Logger.Info("Started with test '{0}'", testName); } } } 

And here is its use

 using Microsoft.VisualStudio.TestTools.UnitTesting; namespace Tests { [TestClass] public class JustTest : TestBase { [TestMethod] public void Fail() { Assert.IsTrue(false); } [TestMethod] public void Pass() { Assert.IsTrue(true); } } } 

here is the part from the log file

 Started with test 'Tests.JustTest.Fail' Test 'Tests.JustTest.Fail' FAILED Started with test 'Tests.JustTest.Pass' Test 'Tests.JustTest.Pass' PASSED 
+1
source share

Update: Now I see what you are getting at. The bad news: I do not use MSTest or MSTest to find out ...

In NUnit you can

 >"nunit-console.exe" API_Tests.dll /out:My.log /labels 

This displays the following log file

 ***** Test.Gumba.API_Tests.Tests.ArithmeticProgression.DummyTest2 Woohoo! made it till test2 ***** Test.Gumba.API_Tests.Tests.ArithmeticProgression.GeneratesTheRightProgressionAsSpecifiedByTheUser Try#0 failed. due to 0. Retrying NUnit.Framework.AssertionException: Expected is <System.Int32[10]>, actual is <System.Int32[0]> <snipped>... 

I was looking at command line switches for MSTest , and the following looks interesting

 mstest /testcontainer:Some.dll /detail:testname 

--------------- previous answer follows -----
To answer your question, the Run method can be executed using the method that the delegate accepts. However, if you could clarify why you need it, perhaps there is a better solution to achieve what you after

eg.

 private void LogAround(Action action) { // log entry with calling method name using StackTrace class action(); // log exit } 

and the challenges will be

 Do( delegate { // test code }); 
0
source share

All Articles