How can I write output from a unit test?

后端 未结 15 968
误落风尘
误落风尘 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:54

    I think it is still actual.

    You can use this NuGet package: Bitoxygen.Testing.Pane

    Call the custom WriteLine method from this library. It creates a Testing pane inside the Output window and puts messages there always (during each test, it runs independently of DEBUG and TRACE flags).

    To make tracing more easy I can recommend to create a base class:

    [TestClass]
    public abstract class BaseTest
    {
        #region Properties
        public TestContext TestContext { get; set; }
    
        public string Class
        {
            get { return this.TestContext.FullyQualifiedTestClassName; }
        }
        public string Method
        {
            get { return this.TestContext.TestName; }
        }
        #endregion
    
        #region Methods
        protected virtual void Trace(string message)
        {
            System.Diagnostics.Trace.WriteLine(message);
    
            Output.Testing.Trace.WriteLine(message);
        }
        #endregion
    }
    
    [TestClass]
    public class SomeTest : BaseTest
    {
        [TestMethod]
        public void SomeTest1()
        {
            this.Trace(string.Format("Yeah: {0} and {1}", this.Class, this.Method));
        }
    }
    

提交回复
热议问题