如何从单元测试写出输出? [英] How to write output from a unit test?

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

问题描述

我的单位中的任何调用都会测试 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() it outputs text in test results.

示例: p>

Example:

    [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天全站免登陆