Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NUnit first test very slow with Entity Framework in Resharper test runner

Is there any solution to the slowness of the first NUnit test when using Entity Framework 4.1 ?

I'm finding it takes about 10 seconds for the first test, the rest of my tests are running in 0.01 seconds (according to Resharper Show Time option)

If I run the tests using the NUnit GUI, again first test takes 10 seconds, but if I re-run the entire set of tests runs in less than 0.5 second ... until I re-compile the solution

I've also got a NUnit tests of a WCF service hosted in IIS, which calls EF, and the first one finishes in about 3 seconds. Then if I re-run tests, it's down to 1 second and, as expected, on recompile the first time is back to 3 seconds.

But the original NUnit tests, which are against a class library with methods calling EF, are always around the 10 second mark, i.e. no decrease in time

Ok, I get that IIS is doing "something" [what?], but in both cases I'm using NUnit to do the calls ... why is one 10+ seconds for first call, the other is 3 seconds and then 1 second on subsequent calls ??

And to really confuse me, I wrote a simple console app that calls one of the EF using methods in the class being tested, and it manages to complete in 1 second.

UPDATE ... the same tests re-written as MSTest tests finish the first test in about 3.5 seconds, which is comparable to the first test run in the NUnit GUI runner, so the issue seems to be the Resharper test runner for NUnit in Visual Studio

UPDATE 2 ... yes, the issue does seem to be the Resharper test runner. TestDriven.NET and Visual Nunit 2010 both are quicker.

UPDATE 3 ... and it's now logged with JetBrains as an issue

like image 970
SteveC Avatar asked Aug 23 '11 11:08

SteveC


People also ask

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.

Is NUnit a test runner?

The NUnit test runner contains the program entry point to run your tests. dotnet test starts the test runner using the unit test project you've created.

How long should unit tests take to run?

Some developers will be happy to run their extensive test suite in three minutes, while others want to go the more radical route and keep it under 60 seconds.


1 Answers

That is common behavior because the first test will have to compile "EF views" - this happens first time you use anything mapped in your model. The same compiled views are then cached and reused. It can be avoided but it requires you manually generate the code for views (by using EdmGen.exe) add that code to your project and compile together with your solution. You will have to do this each time you change anything in EDMX unless you make it as some pre-build action in your project (it will in turn slow your build).

like image 149
Ladislav Mrnka Avatar answered Sep 29 '22 20:09

Ladislav Mrnka