How can I write output from a unit test?

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

    I'm using xUnit so this is what I use:

    Debugger.Log(0, "1", input);
    

    PS: you can use Debugger.Break(); too, so you can see your log in out.

    0 讨论(0)
  • 2020-11-28 21:42

    A different variant of the cause/solution:

    My issue was that I was not getting an output because I was writing the result set from an asynchronous LINQ call to the console in a loop in an asynchronous context:

    var p = _context.Payment.Where(pp => pp.applicationNumber.Trim() == "12345");
    
    p.ForEachAsync(payment => Console.WriteLine(payment.Amount));
    

    And so the test was not writing to the console before the console object was cleaned up by the runtime (when running only one test).

    The solution was to convert the result set to a list first, so I could use the non-asynchronous version of forEach():

    var p = _context.Payment.Where(pp => pp.applicationNumber.Trim() == "12345").ToList();
    
    p.ForEachAsync(payment =>Console.WriteLine(payment.Amount));
    
    0 讨论(0)
  • 2020-11-28 21:44

    Are you sure you're running your unit tests in Debug? Debug.WriteLine won't be called in Release builds.

    Two options to try are:

    • Trace.WriteLine(), which is built into release builds as well as debug

    • Undefine DEBUG in your build settings for the unit test

    0 讨论(0)
  • 2020-11-28 21:48

    I was also trying to get Debug or Trace or Console or TestContext to work in unit testing.

    None of these methods would appear to work or show output in the output window:

        Trace.WriteLine("test trace");
        Debug.WriteLine("test debug");
        TestContext.WriteLine("test context");
        Console.WriteLine("test console");
    

    Visual Studio 2012 and greater

    (from comments) In Visual Studio 2012, there is no icon. Instead, there is a link in the test results called Output. If you click on the link, you see all of the WriteLine.

    Prior to Visual Studio 2012

    I then noticed in my Test Results window, after running the test, next to the little success green circle, there is another icon. I doubled clicked it. It was my test results, and it included all of the types of writelines above.

    0 讨论(0)
  • 2020-11-28 21:50

    Console.WriteLine won't work. Only Debug.WriteLine() or Trace.WriteLine() will work, in debug mode.

    I do the following: include using System.Diagnostics in the test module. Then, use Debug.WriteLine for my output, right click on the test, choose Debug Selected Tests. The result output will now appear in the Output window below. I use Visual Studio 2017 vs 15.8.1, with the default unit test framework VS provides.

    0 讨论(0)
  • 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));
        }
    }
    
    0 讨论(0)
提交回复
热议问题