Mocking 'System.Console' behaviour

前端 未结 5 1904
南方客
南方客 2020-12-06 04:45

Is there a standard way of making a C# console application unit-testable by programming against an interface, rather than System.Console?

For example, using an ICons

相关标签:
5条回答
  • 2020-12-06 05:23

    I think your approach with an interface would work, and I don't think I would make use of events. Assuming the application does not accept user input other than command line parameters, I would probably use something like this to wrap Console.Write/Console.WriteLine:

    public interface IConsoleWriter
    {
        void Write(string format, params object[] args);
        void WriteLine(string format, params object[] args);
    }
    

    To test, I would either create a TestConsoleWriter that would store all writes into a buffer that I could then assert against, or I would create a mock and verify that Write or WriteLine was called with the parameters I expected. If your application is going to be doing a ton of writing to the console (say +100 MB or so of output), then using a mock would probably be preferable for performance reasons, but otherwise I would say pick whichever method you think would be easier to work with.

    This approach does have a few limitations, however. If you are using any assemblies that you cannot modify and they write to the console, then you won't see that output since you can't force those classes to use your IConsoleWriter. Another problem is that the Write and WriteLine methods have 18 or so overloads, so you may be wrapping alot of methods. To get around these limitations, you may just want to use the Console.SetOut method to redirect the console output to your own TextWriter while testing.

    Personally, I think I would take the SetOut approach. It would just be one line you have to add at the beginning of your unit tests (or possibly in a SetUp method) and you can just assert against what is written into the TextWriter.

    0 讨论(0)
  • 2020-12-06 05:26

    I recommend using moles.

    Mainly because I prefer letting your design determine your interfaces and classes, rather than your testing.

    0 讨论(0)
  • 2020-12-06 05:43

    You want to change the stream that console writes to in your unit tests. Then you can put in a mock stream or whatever. Check this post by Mark Seemann on testing Console:

    http://blogs.msdn.com/b/ploeh/archive/2006/10/21/consoleunittesting.aspx

    0 讨论(0)
  • 2020-12-06 05:43

    This code will work if you use just one thread:

    [TestClass]
    public class MyTests
    {
        private StringBuilder output;
        private StringWriter tempOutputWriter;
        private TextWriter originalOutputWriter;
    
        [TestInitialize]
        public void InitializeTest()
        {
            this.originalOutputWriter = Console.Out;
            this.tempOutputWriter = new StringWriter();
            Console.SetOut(tempOutputWriter);
            this.output = tempOutputWriter.GetStringBuilder();
        }
    
        [TestCleanup]
        public void CleanupTest()
        {
            Console.SetOut(originalOutputWriter);
            this.tempOutputWriter.Dispose();
        }
    
        [TestMethod]
        public void Test1()
        {
            Program.Main(new string[] { "1", "2", "3" });
            string output = this.output.ToString();
            ...
            this.output.Clear();
        }
    
        [TestMethod]
        public void Test2()
        {
            Program.Main(new string[] { "4", "5", "6" });
            string output = this.output.ToString();
            ...
            this.output.Clear();
        }
    }
    
    0 讨论(0)
  • 2020-12-06 05:48

    I happened to trip over this thread last night: Using reflection to override virtual method tables in C#.

    @paulo came up with the answer: LinFu by Philip Laureano.

    The usage example on Philip Laureano's Development Blog: Intercepting Console.WriteLine guides you through an example of how to intercept calls to the Console.WriteLine method, and (in this case) perform some additional operations as well... AOP for .NET... Very Noice, IMHO!

    LinFu might be just the ticket, as it has no presence within the thing being intercepted, so you can "intercept and modify" the behaviour of calls to third-party-vendor's assemblies "within your context", but without any possibility of affecting the ACTUAL behaviour of the intercepted classes outside of that context... so LinFu sounds like a darn good place to start implementing a "generic testing-framework for console apps".

    You MAY even be able to leverage one of the existing unit-testing frameworks as well. I'd be looking for an open-source framework. NUnit springs to mind. Pun intended.

    Good luck with it anyway, it's an interesting project. Keep us posted, K?

    Cheers. Keith.

    0 讨论(0)
提交回复
热议问题