<small id='cl1fo'></small><noframes id='cl1fo'>

    <bdo id='cl1fo'></bdo><ul id='cl1fo'></ul>
  • <legend id='cl1fo'><style id='cl1fo'><dir id='cl1fo'><q id='cl1fo'></q></dir></style></legend><tfoot id='cl1fo'></tfoot>
    <i id='cl1fo'><tr id='cl1fo'><dt id='cl1fo'><q id='cl1fo'><span id='cl1fo'><b id='cl1fo'><form id='cl1fo'><ins id='cl1fo'></ins><ul id='cl1fo'></ul><sub id='cl1fo'></sub></form><legend id='cl1fo'></legend><bdo id='cl1fo'><pre id='cl1fo'><center id='cl1fo'></center></pre></bdo></b><th id='cl1fo'></th></span></q></dt></tr></i><div id='cl1fo'><tfoot id='cl1fo'></tfoot><dl id='cl1fo'><fieldset id='cl1fo'></fieldset></dl></div>

      1. 从单元测试中获取发送到 Console.Out 的输出?

        时间:2023-08-27

        <small id='wv4ji'></small><noframes id='wv4ji'>

        <tfoot id='wv4ji'></tfoot>

            <legend id='wv4ji'><style id='wv4ji'><dir id='wv4ji'><q id='wv4ji'></q></dir></style></legend>

            • <bdo id='wv4ji'></bdo><ul id='wv4ji'></ul>
              • <i id='wv4ji'><tr id='wv4ji'><dt id='wv4ji'><q id='wv4ji'><span id='wv4ji'><b id='wv4ji'><form id='wv4ji'><ins id='wv4ji'></ins><ul id='wv4ji'></ul><sub id='wv4ji'></sub></form><legend id='wv4ji'></legend><bdo id='wv4ji'><pre id='wv4ji'><center id='wv4ji'></center></pre></bdo></b><th id='wv4ji'></th></span></q></dt></tr></i><div id='wv4ji'><tfoot id='wv4ji'></tfoot><dl id='wv4ji'><fieldset id='wv4ji'></fieldset></dl></div>
                    <tbody id='wv4ji'></tbody>

                  本文介绍了从单元测试中获取发送到 Console.Out 的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在使用 NUnit 在 C# 中构建单元测试,我想测试主程序是否根据命令行参数实际输出正确的输出.

                  I am building a unit test in C# with NUnit, and I'd like to test that the main program actually outputs the right output depending on the command line arguments.

                  有没有办法从调用 Program.Main(...) 的 NUnit 测试方法中获取写入 Console.OutConsole.Error 以便我可以验证它?

                  Is there a way from an NUnit test method that calls Program.Main(...) to grab everything written to Console.Out and Console.Error so that I can verify against it?

                  推荐答案

                  你可以重定向 Console.In, Console.OutConsole.Error 到自定义 StringWriters,像这样

                  You can redirect Console.In, Console.Out and Console.Error to custom StringWriters, like this

                  [TestMethod]
                  public void ValidateConsoleOutput()
                  {
                      using (StringWriter sw = new StringWriter())
                      {
                          Console.SetOut(sw);
                  
                          ConsoleUser cu = new ConsoleUser();
                          cu.DoWork();
                  
                          string expected = string.Format("Ploeh{0}", Environment.NewLine);
                          Assert.AreEqual<string>(expected, sw.ToString());
                      }
                  }
                  

                  请参阅这篇博文了解全文详情.

                  这篇关于从单元测试中获取发送到 Console.Out 的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:控制台应用程序 C# 中的 App.Config 文件 下一篇:从 Winforms 控制 cmd.exe

                  相关文章

                  最新文章

                  <small id='civW7'></small><noframes id='civW7'>

                  • <bdo id='civW7'></bdo><ul id='civW7'></ul>
                  <i id='civW7'><tr id='civW7'><dt id='civW7'><q id='civW7'><span id='civW7'><b id='civW7'><form id='civW7'><ins id='civW7'></ins><ul id='civW7'></ul><sub id='civW7'></sub></form><legend id='civW7'></legend><bdo id='civW7'><pre id='civW7'><center id='civW7'></center></pre></bdo></b><th id='civW7'></th></span></q></dt></tr></i><div id='civW7'><tfoot id='civW7'></tfoot><dl id='civW7'><fieldset id='civW7'></fieldset></dl></div>

                  1. <tfoot id='civW7'></tfoot>

                    1. <legend id='civW7'><style id='civW7'><dir id='civW7'><q id='civW7'></q></dir></style></legend>