指定要运行的 NUnit 测试

问题描述 投票:0回答:6

我有一个 NUnit 项目,创建一个用于运行测试的控制台应用程序。入口点如下所示:

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        string[] my_args = { Assembly.GetExecutingAssembly().Location };

        int returnCode = NUnit.ConsoleRunner.Runner.Main(my_args);

        if (returnCode != 0)
            Console.Beep();

    }
}

如果我只想运行这个测试,我可以传入什么作为参数:

[TestFixture]
public class EmailNotificationTest
{
    [Test]
    public void MailerDefaultTest()
    {
        Assert.IsTrue(false);
    }
}

显然这是受支持的,而且同样明显我不知道该怎么做。

更新

看起来像 v3+,可以使用

--test
选项,根据 文档

c# nunit
6个回答
21
投票

最新版本 (NUnit 3) 允许调试测试并指定要执行的测试。

调试

--debug
选项启动调试器来调试测试,例如:

nunit3-console.exe "C:\path\to\the\tests.dll" --debug

过滤器测试

现在您可以通过多种不同的方式来选择要运行的测试。第一个选项是

--test=NAMES
。将此选项与
--debug
结合使用,您可以轻松地仅调试一个测试,例如:

nunit3-console.exe "C:\path\to\the\tests.dll" --debug --test="EmailNotificationTest.MailerDeSecondTest" 

如果类有命名空间,请不要忘记它。

使用

--testlist=PATH
选项,您可以运行文件中指定的所有测试,例如:

nunit3-console.exe "C:\path\to\the\tests.dll" --debug --testlist="testnames.txt" 

还有

--where=EXPRESSION
选项指示将运行哪些测试。此选项旨在扩展或替换之前的
--test
--include
--exclude
选项。如果您想了解有关此选项的更多信息,请查看官方文档


16
投票

您可以使用

[Category("RunOnlyThis")]
属性标记您的测试,然后告诉 NUnit 仅运行与此特定类别匹配的测试:

 /include:RunOnlyThis

是您需要添加到控制台运行程序参数的属性。更多这里


5
投票

您可以使用 NUnit 控制台的 /run 开关来指定要运行的测试。

像这样:

/run:namespace.classname.functionName

例如

nunit-console.exe "C:\UnitTests.dll" /run:UnitTests.EmailNotificationTest.MailerDefaultTest

4
投票

正如@Toto所说,使用NUnit Gui,你可以挑选。

enter image description here


2
投票

NUnit 附带一个应用程序,该应用程序可以启动您想要的测试。它真的很有用,而且你不需要编写代码来运行测试。


0
投票

使用 NUnit 时,您可以使用 NUnit Console 运行测试,如下所示:

NUNIT3-CONSOLE.exe [inputfiles] [options]

要运行特定测试,您可以使用

--test=FULLNAMES
选项或
--testlist=FILE
选项。

--test=FULLNAMES
:要运行或探索的测试的全名的逗号分隔列表。该选项可以重复。请注意,保留此选项是为了向后兼容。现在可以使用 --where 选项来代替。
--testlist=FILE
:文件的名称(或路径),其中包含要运行或探索的测试列表,每行一个。还可能包括注释行,由第一列中的
#
表示。

更多信息可以在 NUnit 文档网站

找到
© www.soinside.com 2019 - 2024. All rights reserved.