如何编写单元测试的输出? [英] How can I write output from a unit test?

查看:306
本文介绍了如何编写单元测试的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在单元测试中对Debug.Write(line)Console.Write(Line)的任何调用都只会在调试时被跳过,并且永远不会打印输出.从我正在使用的类中调用这些函数可以正常工作.

Any call in my unit tests to either Debug.Write(line) or Console.Write(Line) simply gets skipped over while debugging and the output is never printed. Calls to these functions from within classes I'm using work fine.

我知道单元测试是自动化的,但是我仍然希望能够从单元测试中输出消息.

I understand that unit testing is meant to be automated, but I still would like to be able to output messages from a unit test.

推荐答案

尝试使用TestContext.WriteLine()在测试结果中输出文本.

Try using TestContext.WriteLine() which outputs text in test results.

示例:

    [TestClass]
    public class UnitTest1
    {
        private TestContext testContextInstance;

        /// <summary>
        ///  Gets or sets the test context which provides
        ///  information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get { return testContextInstance; }
            set { testContextInstance = value; }
        }

        [TestMethod]
        public void TestMethod1()
        {
            TestContext.WriteLine("Message...");
        }
    }

MSDN 作为测试框架会自动设置属性,然后您可以在单元测试中使用该属性."

The "magic" is described in MSDN as "The test framework automatically sets the property, which you can then use in unit tests."

这篇关于如何编写单元测试的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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