代码之家  ›  专栏  ›  技术社区  ›  Dror Helper

如何在MSTest中记录单元测试条目和离开

  •  3
  • Dror Helper  · 技术社区  · 14 年前

    显然,我不想在每个测试的开始和结束时添加自定义日志代码-这只会使测试无法读取,而且看起来需要付出很大的努力(我有500个测试)

    使用 似乎是个好办法,但我没法知道考试的名字。

    有人知道怎么做吗?

    3 回复  |  直到 14 年前
        1
  •  3
  •   Aseem Bansal    14 年前

    在MSTest中,测试用例的名称在 test context property . 因此,要在test initialize(以及test cleanup)方法中访问它,可以使用如下方法:-

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

    亚森班萨尔

        2
  •  1
  •   Andrej Jeznik    11 年前

    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
  •  0
  •   Gishu    14 年前

    更新:好的,现在我明白你的意思了。坏消息是我没有使用MSTest或MSTest设备来查找。。

    在尼特,你可以

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

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