Search code examples
unit-testingvisual-studio-2015timeoutnunitnunit-2.6

Timeboxing NUnit Unit Tests


I've inherited a codebase which has had some bad check-ins -- some of the unit tests are completely hanging and I can't run the entire unit test suite because it will always get stuck on specific tests. -- I would like to take an inventory of those tests that are now hanging.

What's the right way to set a global timeout on all of my tests such that each one is timeboxed to a specific amount of time. (i.e. if I set it to 1 minute, and a test takes 61 seconds, that test is automatically aborted and marked as failed? -- The test runner should then move on to the next test immediately.)

I'm using Visual Studio 2015 Update 1, NUnit 2.6.4, and the NUnit 2.x Test Adapter for Visual Studio.


Solution

  • I believe it is timeout that you want to use here.

    E.g.

    [Test, Timeout(2000)]
    public void PotentiallyLongRunningTest()
    {
        ...
    }
    

    The NUnit documentation seems to indicate it can be set at an assembly level. In AssemblyInfo.cs:

    First:

    using NUnit.Framework; 
    

    Then:

    [assembly: Timeout(1000)]