【问题标题】:Run individual parameterized testcase with nunit-console使用 nunit-console 运行单个参数化测试用例
【发布时间】:2026-01-01 04:05:01
【问题描述】:

我可以从命令行运行一个单独的测试用例,它采用单个字符串值,没有任何问题:

例如/run:Namespace.Class.Method("my input string")

但是对于数字输入,相同的过程似乎对我不起作用

例如:/run:Namespace.Class.Method(1,2,3)

输出将正确的输入列为“要运行的测试”,但实际上并未运行任何测试

编辑:

进一步研究,问题似乎在于测试需要多个参数。使用以下测试文件:

namespace GetTestsProj
{
    [TestFixture]
    class NunitConsoleTest
    {
        [TestCase(1,2,3)]
        [Test, Description("A simple test with parameterized numeric inputs")]
        public void TestNumeric(int a, int b, int c)
        {
            Assert.AreEqual(c, a + b);
        }

        [TestCase("My String")]
        [Test, Description("A simple test with parameterized string input")]
        public void TestSingleString(string a)
        {
            Assert.AreEqual("My String", a);
        }
        [TestCase("String1", "String2")]
        [Test, Description("A simple test with parameterized numeric inputs")]
        public void TestTwoStrings(string a, string b)
        {
            Assert.AreEqual("String1", a);
        }
    }
}

调用 nunit-console.exe /run:GetTestsProj.NunitConsoleTest GetTestsProj\GetTestsProj\bin\debug\GetTestsProj.dll 正确运行所有 3 个测试用例

nunit-console.exe /run:GetTestsProj.NunitConsoleTest.TestNumeric GetTestsProj\GetTestsProj\bin\debug\GetTestsProj.dll 调用正确运行了 1 个测试用例

nunit-console.exe /run:"GetTestsProj.NunitConsoleTest.TestSingleString(\"My String\")" GetTestsProj\GetTestsProj\bin\debug\GetTestsProj.dll 调用正确运行了 1 个测试用例

但是,调用 nunit-console.exe /run:GetTestsProj.NunitConsoleTest.TestNumeric(1,2,3) GetTestsProj\GetTestsProj\bin\debug\GetTestsProj.dll 运行 0 个测试用例

同样,nunit-console.exe /run:"GetTestsProj.NunitConsoleTest.TestTwoStrings(\"String1\",\"String2\")" GetTestsProj\GetTestsProj\bin\debug\GetTestsProj.dll 调用运行 0 个测试用例

虽然 nunit 似乎可以正确识别输入 /run

Selected test(s): GetTestsProj.NunitConsoleTest.TestNumeric(1,2,3)

Tests run: 0, Errors: 0, Failures: 0, Inconclusive: 0, Time: 0 seconds
  Not run: 0, Invalid: 0, Ignored: 0, Skipped: 0

Selected test(s): GetTestsProj.NunitConsoleTest.TestTwoStrings("String1", "String2")

Tests run: 0, Errors: 0, Failures: 0, Inconclusive: 0, Time: 0.0156256 seconds
  Not run: 0, Invalid: 0, Ignored: 0, Skipped: 0

这都是使用 NUnit 2.5.9.10348

我对这是用户错误还是不支持的功能感兴趣。这对我正在尝试做的事情非常有用。

【问题讨论】:

  • 由于这可能是一个错误,我已将其提交给 nunit:link

标签: c# nunit


【解决方案1】:

看起来问题是测试用例列表在“,”字符上拆分,导致参数化测试用例出现明显问题。错误信息是here,有更多信息我会尝试在这里发布结论。

【讨论】:

    【解决方案2】:

    看起来这种行为应该在 NUnit 2.6.0 和更新版本中得到修复。

    如果您仍然像我一样遇到这个问题(这里使用 NUnit 2.6.4),那么您可能需要通过在双引号字符前面放置一个反斜杠来转义它们。

    【讨论】:

      【解决方案3】:

      并不是说它对您的特定问题有帮助,但是您是否知道 JetBrains 的 ReSharper 有一个用于 VS 的 NUnit 测试运行程序?可能还有更多具有它的工具。我有 Resharper,从那以后再也不需要从控制台运行单个测试(此外,它有一大堆很酷的重构工具)。我希望这听起来不像垃圾邮件。我只是想知道为什么您从控制台运行单个测试。

      【讨论】:

      • 我确实有 ReSharper(它很棒),但我试图在 CI 环境中运行这些测试,这意味着我仅限于控制台。我也在运行使用 Selenium 的前端测试。运行单个测试用例有两个原因:1)Selenium 测试倾向于遇到超时或其他间歇性问题,我希望能够多次重新运行失败的测试,以及 2)我想能够让单独的测试用例在不同的虚拟机上运行,​​以帮助并行化流程
      • 好吧,那我恐怕帮不上什么忙了。在我的工作中,我们也运行 CI,但当时只是整个项目,而不是单个测试。您是否考虑过将这些特殊的测试用例转移到自己的项目中,然后像往常一样运行它们?