简体   繁体   English

Visual Studio C#单元测试-报告每次运行的结果

[英]Visual Studio C# unit testing - report results for each run

Is there way to automate reporting of each test run? 有没有办法自动报告每个测试运行?

Now I do this 现在我这样做

[TestMethod]
public void Test1()
{
   ReportTestStarted("Test1");
   // do test like 
   Assert.IsTrue(true,"Fake good test");
   ReportTestFinshed("Test1");
}

with assumption that when test started it should be finished then it is good. 假设测试开始时应该完成就可以了。 ReportTest*() collects all results and then stores them somehow. ReportTest *()收集所有结果,然后以某种方式存储它们。

I hope that it is possible to use Attributes and update [TestMethod] so it will do such work automatically. 我希望可以使用Attributes并更新[TestMethod],以便它将自动执行此类工作。 Any ideas? 有任何想法吗?

Update: Thanks for @Richard's and @Scott hints I had figured out that I can use this code and know result based on this link 更新:感谢@Richard和@Scott的提示,我发现我可以使用此代码并基于此链接知道结果

        public TestContext TestContext { get; set; }
       [TestCleanup]
        public void CleanupTest()
        {
            Console.WriteLine(
                "TextContext.TestName='{0}' {1} ",
                TestContext.TestName,
(Microsoft.VisualStudio.TestTools.UnitTesting.UnitTestOutcome.Passed==TestContext.CurrentTestOutcome?"Pass":"Fail"));
        }

Thanks for comments I have my answer: 感谢您的评论我有我的答案:

        public TestContext TestContext { get; set; }
       [TestCleanup]
        public void CleanupTest()
        {
            Console.WriteLine(
                "TextContext.TestName='{0}' {1} ",
                TestContext.TestName,
(Microsoft.VisualStudio.TestTools.UnitTesting.UnitTestOutcome.Passed==TestContext.CurrentTestOutcome?"Pass":"Fail"));
        }

It provides access to the test name and test result which I can use and store in any way. 它提供对测试名称和测试结果的访问,我可以以任何方式使用和存储它们。 Most important - I do not have to touch my TestMethods. 最重要的是-我不必触摸我的TestMethods。 This approach collects and reports all result without adding extra lines of code to each TestMethod. 这种方法收集并报告所有结果,而无需向每个TestMethod添加额外的代码行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM