如何记录单元测试条目并保留在MSTest中 [英] How to log unit test entry and leave in MSTest

查看:138
本文介绍了如何记录单元测试条目并保留在MSTest中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using MSTest and I want to write log entry before test executes and after it finishes. Obviously I don't want to add custom logging code at the beginning and end of each test - it would only make the test unreadable and seemed like a lot of effort (I have > 500 tests)

使用 TestInitialize TestCleanup 似乎很可行,但我无法获得测试名称.

Using TestInitialize and TestCleanup seemed like the way to go but I can't get the test name.

有人知道怎么做吗?

推荐答案

在MSTest中,测试用例的名称在

In MSTest, the name of the test case is available in the test context property. So to access it in the test initialize (as well as test cleanup) method, you can use something like this: -

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

问候 Aseem Bansal

Regards Aseem Bansal

这篇关于如何记录单元测试条目并保留在MSTest中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆