20

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.

Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254
  • I haven't used the ConsoleRunner, but it probably honors the `[Explicit]` and `[Category]` attributes. – jrummell Nov 03 '11 at 14:06
  • 1
    For those looking for `nunit3-console.exe` see http://stackoverflow.com/questions/37297838/run-individual-test-from-nunit3-console-exe – KCD Feb 01 '17 at 00:52

6 Answers6

21

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.

Sergii Zhevzhyk
  • 4,074
  • 22
  • 28
  • The official doc sadly hasn't specified what `NAMES` really is. Looks like it's the fully qualified classname ? – gideon Nov 28 '16 at 10:20
  • for the --testlist, how do you specify a testlist found in the DLL? i keep getting unable to locate file error. Where do we store these test files? – Tree55Topz Jan 25 '18 at 18:45
  • 1
    @gideon - using nunit3 in C#, the pattern that works for me is: --test="namespace.class-name.method-name". Also, the --debug switch isn't necessary for this – bitcoder May 17 '18 at 14:59
16

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.

k.m
  • 30,794
  • 10
  • 62
  • 86
  • This isn't necessary from the GUI, though... I would expect to be able to be specific without making code changes so that I could localize a specific test like I can do from the GUI. Is this not possible? – Jeremy Holovacs Nov 03 '11 at 14:44
  • @JeremyHolovacs: NUnit doc remains silent about that, I'd suppose console runner doesn't allow it then. Having that in mind, I'd go with attaching debugger - it's pretty easy and you can find how to do that on SO ([for example here](http://stackoverflow.com/questions/759854/how-do-i-run-nunit-in-debug-mode-from-visual-studio)). – k.m Nov 03 '11 at 19:24
  • 1
    I ended up using a `/fixture:EmailNotificationTest` and put my test in its own class. Attaching the debugger each time I tweak something is annoying... but the answer does seem to be "you can't do it". Lame. – Jeremy Holovacs Nov 04 '11 at 19:45
  • How to run a category from cmd using dotnet test? – Mounika May 20 '22 at 13:34
  • Comment for future visitors: This parameter does not work in newer versions. Nunit3 changed the parameters for console runner. – ghord Feb 07 '23 at 10:35
5

You can use /run switch of NUnit console to specify the test that you want to run.

Like this:

/run:namespace.classname.functionName

E.g.

nunit-console.exe "C:\UnitTests.dll" /run:UnitTests.EmailNotificationTest.MailerDefaultTest
digitguy
  • 1,014
  • 2
  • 12
  • 29
4

As @Toto said, use the NUnit Gui, you can pick and choose.

enter image description here

Ta01
  • 31,040
  • 13
  • 70
  • 99
  • Yes this is how I know it is possible, but that is not what I am trying to do. In this case, my latest code changes broke some unit tests, and I am trying to find root cause. If I run the NUnit project as a console app from visual studio, I can put breakpoints in and trace the code, which is what I'm trying to do. – Jeremy Holovacs Nov 03 '11 at 14:05
  • 1
    You can attach visual studio to nunit, your breakpioints will be reach. I do it often. There is also http://www.testdriven.net/ wich provides rightclick functionality in visual studio to launch unit test with visual studio debugger (really useful). – Toto Nov 03 '11 at 14:15
2

An application comes with NUnit, and the application can launch the test you want. It's really useful, and you don't have to write code to run test.

Toto
  • 149
  • 2
  • I really want to automatically attach it to the debugger, which the application does not allow me to do (or at least, not easily). I have a few tests that are failing and I'm trying to see why. – Jeremy Holovacs Nov 03 '11 at 14:03
0

When using NUnit, you can run the tests using NUnit Console like this:

NUNIT3-CONSOLE.exe [inputfiles] [options]

To run the specific tests, you can use --test=FULLNAMES option or --testlist=FILE option.

--test=FULLNAMES: Comma-separated list of FULLNAMES of tests to run or explore. This option may be repeated. Note that this option is retained for backward compatibility. The --where option can now be used instead.
--testlist=FILE: The name (or path) of a FILE containing a list of tests to run or explore, one per line. May also include comment lines, indicated by # in the first column.

More info can be found in the NUnit docs website

Evgenii Kosiakov
  • 545
  • 12
  • 21