21

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.

2
  • I haven't used the ConsoleRunner, but it probably honors the [Explicit] and [Category] attributes.
    – jrummell
    Commented Nov 3, 2011 at 14:06
  • 1
    For those looking for nunit3-console.exe see stackoverflow.com/questions/37297838/…
    – KCD
    Commented Feb 1, 2017 at 0:52

6 Answers 6

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.

3
  • The official doc sadly hasn't specified what NAMES really is. Looks like it's the fully qualified classname ?
    – gideon
    Commented Nov 28, 2016 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
    Commented Jan 25, 2018 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
    Commented May 17, 2018 at 14:59
17

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.

5
  • 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?
    – user610217
    Commented Nov 3, 2011 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).
    – k.m
    Commented Nov 3, 2011 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.
    – user610217
    Commented Nov 4, 2011 at 19:45
  • How to run a category from cmd using dotnet test?
    – Mounika
    Commented May 20, 2022 at 13:34
  • Comment for future visitors: This parameter does not work in newer versions. Nunit3 changed the parameters for console runner.
    – ghord
    Commented Feb 7, 2023 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
4

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

enter image description here

2
  • 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.
    – user610217
    Commented Nov 3, 2011 at 14:05
  • 1
    You can attach visual studio to nunit, your breakpioints will be reach. I do it often. There is also testdriven.net wich provides rightclick functionality in visual studio to launch unit test with visual studio debugger (really useful).
    – Toto
    Commented Nov 3, 2011 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.

1
  • 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.
    – user610217
    Commented Nov 3, 2011 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.