How to log unit test entry and leave in MSTest

拈花ヽ惹草 提交于 2020-01-14 22:47:56

问题


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

Using TestInitialize and TestCleanup seemed like the way to go but I can't get the test name.

Does anyone knows how to do this?


回答1:


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

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

Regards Aseem Bansal




回答2:


We use nLog for logging and an base class for test initialize and test cleanup that logs if a test has passed 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 the use of it

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 log file

Started with test 'Tests.JustTest.Fail'
Test 'Tests.JustTest.Fail' FAILED

Started with test 'Tests.JustTest.Pass'
Test 'Tests.JustTest.Pass' PASSED



回答3:


Update: Ok now I see what you're getting at. Bad news is I don't use MSTest or a MSTest fixture to find out..

In NUnit, you could

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

THis outputs 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 the command line switches for MSTest and the following looks interesting

mstest /testcontainer:Some.dll /detail:testname

--------------- previous answer follows -----
To answer your question to the point, 'Execute around' methods can be done using a method that takes a delegate. However if you could elaborate on why you need this, maybe there is a better solution to achieve whatever it is you're after

e.g.

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

and calls would be

Do( delegate {
  // test code
});


来源:https://stackoverflow.com/questions/3879819/how-to-log-unit-test-entry-and-leave-in-mstest

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!