Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can NUnit expect a timeout?

I would like to test a method that I am expecting to block in a specific situation.

I tried a combination of the TimeoutAttribute and ExpectedExceptionAttribute:

[Test]
[Timeout(50), ExpectedException(typeof(ThreadAbortException))]
public void BlockingCallShouldBlock()
{
    this.SomeBlockingCall();
}

Unfortunately this does not work as the ThreadAbortException I was reading about here seems to get caught by NUnit itself.

Is there a way to expect timeouts (with NUnit)?

like image 793
CodeFox Avatar asked Oct 16 '13 14:10

CodeFox


People also ask

What are the things can be mocked for unit testing in NUnit?

Objects that do not have an interface (as required for mock-object pattern) Objects that are instantiated in the code being tested.

Does SetUp run before every test NUnit?

Inheritance. The SetUp attribute is inherited from any base class. Therefore, if a base class has defined a SetUp method, that method will be called before each test method in the derived class.

Is NUnit a test runner?

The nunit.exe program is a graphical runner. It shows the tests in an explorer-like browser window and provides a visual indication of the success or failure of the tests.


1 Answers

For a problem like this, I would probably use Task and Task.Wait(int) or Task.Wait(TimeSpan). For example:

[Test]
public void BlockingCallShouldBlock()
{
    var task = Task.Run(() => SomeBlockingCall());
    var completedInTime = task.Wait(50); // Also an overload available for TimeSpan
    Expect(completedInTime, Is.False);
}

Be warned however, this will invoke SomeBlockingCall on a background thread, but for the majority of unit tests, this is a non-issue.

like image 130
Lukazoid Avatar answered Oct 04 '22 02:10

Lukazoid