简体   繁体   English

指定要运行的NUnit测试

[英]specify NUnit test to run

I have an NUnit project creating a Console Application for running tests. 我有一个NUnit项目,用于创建用于运行测试的控制台应用程序。 The entry point looks like this: 入口点如下所示:

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();

    }
}

What can I pass in as an argument if I wanted to run this one test ONLY: 如果我只想运行这个测试,我可以作为参数传递什么:

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

Clearly this is supported, and just as clearly I have no idea how to do it. 显然这是支持的,同样明显我不知道如何做到这一点。

UPDATE UPDATE

It looks like with v3+, this is possible with the --test option, per the documentation . 看起来像v3 +, 根据文档可以使用--test选项。

The latest version (NUnit 3) allows to debug tests and also to specify test(s) for execution. 最新版本(NUnit 3)允许调试测试并指定执行测试。

Debug 调试

The --debug option launches debugger to debug tests, for example: --debug选项启动调试器以调试测试,例如:

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

Filter tests 过滤测试

Now you have a number of different ways to select test(s) to run. 现在,您有多种不同的方法可以选择要运行的测试。 The first option is --test=NAMES . 第一个选项是--test=NAMES Combining this option and --debug you can easily debug only one test, for example: 结合此选项和--debug您只需轻松调试一个测试,例如:

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

Don't forget about the namespace if the class has it. 如果类具有它,请不要忘记命名空间。

Using --testlist=PATH option you can run all tests specified in a file, for example: 使用--testlist=PATH选项,您可以运行文件中指定的所有测试,例如:

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

There is also --where=EXPRESSION option indicating what tests will be run. 还有--where=EXPRESSION选项,指示将运行哪些测试。 This option is intended to extend or replace the earlier --test , --include and --exclude options. 此选项旨在扩展或替换早期的--test , - --include--exclude选项。 Please check the official documentation if you want to know more about this option. 如果您想了解有关此选项的更多信息,请查看官方文档

You can mark your test with [Category("RunOnlyThis")] attribute, and then tell NUnit to run tests only matching this specific category: 您可以使用[Category("RunOnlyThis")]属性标记测试,然后告诉NUnit运行仅匹配此特定类别的测试:

 /include:RunOnlyThis

is the attribute you need to add to console runner arguments. 是您需要添加到控制台运行程序参数的属性。 More here . 更多这里

You can use /run switch of NUnit console to specify the test that you want to run. 您可以使用/运行 NUnit控制台的开关来指定要运行的测试。

Like this: 像这样:

/run:namespace.classname.functionName

Eg 例如

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

As @Toto said, use the NUnit Gui , you can pick and choose. 正如@Toto所说,使用NUnit Gui ,你可以挑选。

在此输入图像描述

An application comes with NUnit, and the application can launch the test you want. NUnit附带一个应用程序,应用程序可以启动您想要的测试。 It's really useful, and you don't have to write code to run test. 它非常有用,您不必编写代码来运行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM