如何从单元测试写出输出?

如何从单元测试写出输出?

问题描述:

我的单位中的任何调用都会测试 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."