Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

specify NUnit test to run

Tags:

c#

nunit

I have an NUnit project creating a Console Application for running tests. 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

It looks like with v3+, this is possible with the --test option, per the documentation.

like image 943
Jeremy Holovacs Avatar asked Nov 03 '11 13:11

Jeremy Holovacs


People also ask

How do I run nunit3 console exe?

Open a powershell window and run nunit3-console.exe with "--test" option set to reference the specific test you want to run (including namespace and class). and finally, provide the location of the assembly where the test can be found.

How does NUnit decide what order to run tests in?

There is no facility in NUnit to order tests globally. Tests with an OrderAttribute argument are started before any tests without the attribute. Ordered tests are started in ascending order of the order argument. Among tests with the same order value or without the attribute, execution order is indeterminate.


2 Answers

The latest version (NUnit 3) allows to debug tests and also to specify test(s) for execution.

Debug

The --debug option launches debugger to debug tests, for example:

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. Combining this option and --debug you can easily debug only one test, for example:

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:

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. This option is intended to extend or replace the earlier --test, --include and --exclude options. Please check the official documentation if you want to know more about this option.

like image 179
Sergii Zhevzhyk Avatar answered Sep 19 '22 13:09

Sergii Zhevzhyk


You can mark your test with [Category("RunOnlyThis")] attribute, and then tell NUnit to run tests only matching this specific category:

 /include:RunOnlyThis

is the attribute you need to add to console runner arguments. More here.

like image 26
k.m Avatar answered Sep 20 '22 13:09

k.m