Troubleshooting slow unit test runs #4466
-
Hi, Hoping the community can help here. We are using NUnit (3.13.3) and NUnit3TestAdapter (4.5.0) in all our test projects, we have roughly 5 test projects with around 10.000+ tests in them, of them around 9.000 is located in one of the projects, our core project. All tests are pure unit tests, no integration tests in these projects. We are using NSubstitute primarly, but a few legacy tests is using Moq. A small part of the tests use [SetUp] or [SetUpFixture] but we are trying to get away from that. All projects are .net 6. Currently our tests take around 6m 30s to run on the fastest dev machines and around 10m on our build agents, so we are looking to increase performance and one of the things we are looking into is running tests in parallel with assembly level attribute like This for some reason doesn't speed up our test runs, it actually made them a little bit slower, which we do not understand. We have tried making example projects with a couple of hundred tests in them, purely with just Thread.Sleep(..) in them, and they ran as fast as expected. We then started logging the tests results to a trx file, importing said file in excel so we could see the duration of each test. This was an eye opener, some of the tests took 30+ seconds to run, but if you run them individually or even as part of a test fixture with a lot of tests in them they run instant (< 1ms effectively). It makes no difference if we run the tests on commandline with We did notice that there is some overhead in creating mocks with NSubstitute for example Substitute.For() takes around 60-70ms for most cases, but that is caused by Castle.Core creating proxies, and using ParallelScope.Children seem to run everything in new threads so it cannot re-use its internal caches, hence why we are using ParallelScope.Fixtures, but this doesn't even remote explain the 30+second test durations we are seeing. Is there any way to troubleshoot this? Or does anyone have any good ideas around this? I think I have read all documentation there is around NUnit for logging things or whatever, but no hints yet. Want to thank the developers for making NUnit, what an amazing piece of work. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 13 replies
-
Assembly parallelization is handled by the testhost, not by nunit. If you add a runsettings file (you can install the template using https://marketplace.visualstudio.com/items?itemName=OsirisTerje.Runsettings-19151) , select the parallell template, and you have the correct settings. There is a blog (a bit old now, but still correct, some links seems dead) https://devblogs.microsoft.com/devops/parallel-and-context-sensitive-test-execution-with-visual-studio-2015-update-1/ . The setting you're looking for is PS: The template also ensures you don't activate code coverage, which will slow down everything. See https://devblogs.microsoft.com/devops/parallel-and-context-sensitive-test-execution-with-visual-studio-2015-update-1/#why-dont-we-include-code-coverage |
Beta Was this translation helpful? Give feedback.
If the number of tests are >2000 you're hitting the default setting AssemblySelectLimit, which is at 2000. See https://docs.nunit.org/articles/vs-test-adapter/Tips-And-Tricks.html#assemblyselectlimit which also explains the side effects of this setting, which is that the category setting is skipped (in VS).
I suggest you raise this limit in your case.
Another tip: Set the NUnit.Verborsity to 5. You will then see all the settings for the adapter listed, including the ASL, and others that might be interesting.