【问题标题】:NUnit3TestExecutor converting tests but is not running themNUnit3TestExecutor 转换测试但没有运行它们
【发布时间】:2026-01-03 16:05:02
【问题描述】:

试图让我的单元测试运行,但在单击“运行测试”后,我的输出中只得到“NUnit3TestExecutor 转换了 1 个 NUnit 测试用例”。测试尝试运行,但最终永远加载,直到我不得不手动取消该过程。知道是什么原因造成的吗?

using NUnit.Framework;
    using CalculatorApp;
    using System.IO;
    using System;
    using NUnit;



    namespace Tests
    {
        public class Tests
        {
            [SetUp]
            public void Setup()
            {
            }

            [Test]
            public void Test1()
            {

                using (StringWriter sw = new StringWriter())
                {
                    Console.SetOut(sw);
                    CalculatorApp.Program.Input1();
                    string expected = string.Format("Type a number, and then press Enter", Environment.NewLine);
                    Assert.AreEqual(expected, sw.ToString());
                    sw.Close();
                }
            }
        }
    }

【问题讨论】:

    标签: unit-testing nunit console-application nunit-3.0


    【解决方案1】:

    根据您对Console.SetOut 的使用,我假设您正在测试一个控制台程序。我还假设它等待用户的输入。

    不幸的是,它没有从您的代码中获得任何输入,所以它挂起。

    通过驱动输入和输出来测试控制台应用程序是可能的,但很复杂。您通常需要在单独的进程中启动它,监控标准和错误输出并驱动输入。大多数人不会以这种方式进行测试——至少那些有单元测试经验的人不会。

    更好的方法是测试执行计算的后端。

    我应该提到另一个复杂的情况... NUnit 本身控制控制台输出。因此,您的测试需要保存当前输出设置并在退出前恢复它 - 否则,NUnit 的继续处理将中断。

    【讨论】:

      最近更新 更多