locked
How to test value entered in Console.Write() method Console application RRS feed

  • Question

  • Hi All.

    How create Unit test project to test value entered in Console,Write() method Console Framework application? If that is possible. Can explain more detail?

    Thanks

    Thursday, January 16, 2020 5:02 PM

All replies

  • Hi zleug,

    Welcome to MSDN forum.

    For your requirement, you could refer to this thread Grabbing the output sent to Console.Out from within a unit test?

    I have write a simple sample for you to easily test.

    Below is program of console app project.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                int a = 8;
                Test t = new Test();
                t.Testmethod(a);
            }
    
        }
    
        public class Test
        {
            public void Testmethod(int b)
            {
                System.Console.Write(b);
            }
            
        }
    }

    Below is unit test project for console app.

    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using ConsoleApp;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    
    namespace ConsoleApp.Tests
    {
        [TestClass()]
        public class testTests
        {
            [TestMethod()]
            public void TestmethodTest()
            {
                using (StringWriter sw = new StringWriter())
                {
                    Console.SetOut(sw);
    
                    Test t = new Test();
                    t.Testmethod(8);
    
                    int expected = 8;
                    Assert.AreEqual(expected.ToString(), sw.ToString());
                }
            }
        }
    }

    Hope this could help you and any feedback will be expected.

    Best Regards,

    Tianyu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, January 17, 2020 7:30 AM
  • Hi zleug,

    Sorry to bother you.

    Do you have any update? If your issue still persists, feel free to contact us.

    Looking forward to hearing from you.

    Sincerely,

    Tianyu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 20, 2020 4:32 AM