简体   繁体   中英

Visual Studio Unit Tests running slower on TFS Build

My project has 1000+ unit tests that, in a local machine, all run in less than 10 seconds. But when they run on TFS Build, some tests run significantly slower than others. 3 of them run in about 1-2 minutes, other 4 in 5-30 seconds, and the others in fractions of a second. I've noticed that all those slower tests use fakes from Microsoft Fakes, and each one of them is the first to run in it's class. But a lot of the other tests also use fakes (some more intensively) and run in regular time. I would like to know what may be causing that slowdown and how can I fix it.

Edit: I've noticed that every slower test runs after a mockless test. Maybe that slowdown is caused by the initialization of the ShimsContext. In my test classes, the ShimsContext is created and disposed on TestInitialize and TestCleanup methods. Does that affects significantly the performance?

First off all I would strongly recommend you move away from shims. They are a crutch, and aside from a very few scenarios, simply not needed. Design your code for testability, and you will find you can do without them.

Second, shims are not thread safe, and cannot be used concurrently safely. Likely this is why you are seeing the really slow run times.

Either your local is running things concurrently when it shouldn't (MS says it isn't safe, but not enforced), and the build server isn't.

Or the build server is trying to be parallel and it is causing issues.

Tweak your concurrency settings to disable it and see what effect that has on your runtime.

Refer following links : https://softwareengineering.stackexchange.com/questions/184834/how-do-we-make-unit-tests-run-fast

http://arlobelshee.com/the-no-mocks-book/

The links say that making tests fast can be hard. Decoupling is key. Mocks/fakes are OK, but one can do better by refactoring to make mocks/fakes unnecessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM