【问题标题】:How to log unit test entry and leave in MSTest如何在 MSTest 中记录单元测试进入和离开
【发布时间】:2011-04-22 05:51:25
【问题描述】:

我正在使用 MSTest,我想在测试执行之前和完成之后编写日志条目。 显然我不想在每个测试的开始和结束时添加自定义日志记录代码 - 它只会使测试不可读并且看起来需要付出很多努力(我有超过 500 个测试)

使用 TestInitialize 和 TestCleanup 似乎是可行的方法,但我无法获得测试名称。

有人知道怎么做吗?

【问题讨论】:

  • 为什么需要测试名称?如果您能告诉我们您尝试使用日志记录实现的目标,也许我可以给出更好的答案。
  • @Gishu - 我有一个来自所有测试的长日志文件,我想在该日志中查看当前条目属于哪个测试。通过在每次测试之前和之后添加日志记录,我可以“绑定”测试的信息

标签: logging mstest


【解决方案1】:

在 MSTest 中,测试用例的名称可在 test context property 中找到。因此,要在测试初始化​​(以及测试清理)方法中访问它,您可以使用如下内容:-

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

【讨论】:

    【解决方案2】:

    我们使用nLog 进行日志记录,使用基类进行测试初始化​​和测试清理,记录测试是否通过。

    这是基类:

    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);
            }
        }
    }
    

    这是它的用途

    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);
            }
        }
    }
    

    这是日志文件中的部分

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

    【讨论】:

    • 添加一些对代码作用的解释可以大大改善您的答案。
    【解决方案3】:

    更新:好的,现在我明白你的意思了。坏消息是我不使用 MSTest 或 MSTest 夹具来找出答案。

    在 NUnit 中,你可以

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

    这会输出以下日志文​​件

    ***** 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>...
    

    我是 MSTest 的 looking at the command line switches,以下看起来很有趣

    mstest /testcontainer:Some.dll /detail:testname
    

    --------------- 上一个答案如下 -----
    为了回答您的问题,可以使用接受委托的方法来完成“执行周围”方法。但是,如果您可以详细说明为什么需要这个,也许有更好的解决方案来实现您所追求的目标

    例如

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

    电话会是

    Do( delegate {
      // test code
    });
    

    【讨论】:

    • 不幸的是,这意味着我需要将此代码添加到所有可能存在和未来的测试中,这会损害它们的可读性。我还需要确保团队中的所有开发人员在他们的所有测试中都使用它......我想要一个解决方案,“以芳香方式”将日志记录添加到我的所有测试中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 2010-10-17
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多