2010-10-07 23 views
3

我正在使用MSTest,并且希望在测试执行之前和完成之后写入日志条目。 很显然,我不希望在开始和每个测试的末尾添加自定义日志代码 - 这将只会使测试不可读似乎是一个很大的努力(我有> 500个测试)如何记录单元测试条目并离开MSTest

使用TestInitializeTestCleanup似乎是要走的路,但我无法获得测试名称。

有谁知道如何做到这一点?

+0

的一部分,为什么你需要的测试名称?如果您可以告诉我们您使用日志记录要实现的目标,也许我可以给出更好的答案。 – Gishu 2010-10-07 08:32:55

+0

@Gishu - 我有一个来自所有测试的长日志文件,我希望在该日志中看到当前条目属于哪个测试。通过在每次测试之前和之后添加日志记录,我可以“绑定”测试的信息 – 2010-10-07 09:17:43

回答

3

在MSTest的,测试案例的名称是test context property可用。所以要访问它在测试初始化​​(以及测试清理)方法,你可以使用这样的事情: -

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

问候 Aseem邦萨尔

0

更新:好吧,现在我明白你在看什么。坏消息是我不使用的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>... 

looking at the command line switches对于MSTest和以下看起来很有趣

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

不幸的是,这意味着我需要将此代码添加到所有可能会影响其可读性的当前和未来测试中。我还需要确保团队中的所有开发人员在他们的所有测试中都使用这个... 我想要一个“芳香”地将日志记录添加到我的所有测试中的解决方案 – 2010-10-07 09:12:22

+0

@Dror - 已更新的答案..您需要一些来自MSTest的支持。希望它存在... – Gishu 2010-10-07 10:33:04

1

我们使用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 
+1

你的答案可以通过添加一些你的代码的解释来大大改善。 – Henrik 2013-01-09 09:45:10