How can I write output from a unit test?

后端 未结 15 987
误落风尘
误落风尘 2020-11-28 20:56

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 print

15条回答
  •  误落风尘
    2020-11-28 21:36

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

    Example:

    [TestClass]
    public class UnitTest1
    {
        private TestContext testContextInstance;
    
        /// 
        /// Gets or sets the test context which provides
        /// information about and functionality for the current test run.
        /// 
        public TestContext TestContext
        {
            get { return testContextInstance; }
            set { testContextInstance = value; }
        }
    
        [TestMethod]
        public void TestMethod1()
        {
            TestContext.WriteLine("Message...");
        }
    }
    

    The "magic" is described in MSDN:

    To use TestContext, create a member and property within your test class [...] The test framework automatically sets the property, which you can then use in unit tests.

提交回复
热议问题