【问题标题】:How to get test result status from MSTest?如何从 MSTest 获取测试结果状态?
【发布时间】:2012-11-02 19:08:48
【问题描述】:

在 NUnit 中,我可以从context.Result.State 获取测试结果。如果是NUnit.Framework.TestState.Success,那我就知道测试通过了。

在 MSTest 中,我如何获取该信息?

我看到context.Properties.Keys,但没有一个人说测试结果的状态。

【问题讨论】:

    标签: unit-testing mstest vs-unit-testing-framework


    【解决方案1】:

    TestCleanup 方法中使用TestContext.CurrentTestOutcome 属性:

    [TestClass]
    public class UnitTest
    {
        private TestContext TestContext { get; set; }
    
        [TestCleanup]
        public void TestCleanup()
        {
            if (TestContext.CurrentTestOutcome == UnitTestOutcome.Passed)
                //do something
        } 
    
        [TestMethod]
        public void TestMethod()
        {
        }
    }
    

    【讨论】:

    • 你能告诉我如何在 TestCleanup 方法中获取实际原因/日志(意味着我的测试用例失败的原因以及在哪个行号上)?
    • 我有 3 个测试用例。 TestContext.CurrentTestOutcome == UnitTestOutcome.Passed 将如何在 testcleanup() 中为 3 个测试用例工作?就像我怎样才能得到所有 3 三个测试用例的结果?
    • 当我尝试这个时,将 TestContext 作为私有属性会阻止测试运行。当公开时一切都好
    猜你喜欢
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    相关资源
    最近更新 更多