Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with slow unit tests while doing TDD in large c# project

Using visual studio 2017 NUnit and the resharper test runner, how do you maintain a good unit test speed when doing TDD in a large c# project (5000+) tests. Even if each of those tests takes only 5ms, that's 25 seconds which is quite slow for a TDD cycle.

Our tests don't call the database nor do they call external web services. They only test business logic.

I have found that using moq, doing a Mock.Setup() alone takes almost 1ms. Since we might have a few moq setups call per tests, this is the primary culprit for our slow unit tests.

Is there any way to speed up unit tests speed? Is there any mocking libraries faster than moq? Or maybe another test runner that is faster?

like image 368
Chocoman Avatar asked Dec 04 '18 18:12

Chocoman


1 Answers

You are going down the wrong rabbit whole: the overall runtime of all your unit tests is still in a very reasonable range!

While doing development (maybe using TDD) you don't care about all unit tests. You only care about those that are relevant to the current component/package/... !

As in: when you make a change in file A, you probably want to (manually) run all unit tests for the directory A lives in. You make another small change, you run these tests again.

Then, later on, when you think: "I am done for now", then you invoke all unit tests, to ensure you didn't break something on the other end of the building by rearranging the furniture in that room over here.

So, the answer is: you are fine, don't worry.

We have 5000+ Java unit tests. On our fastest build server, it can take about 10 minutes to work them all. But that is still ok. The backend build still comes back after 20 minutes and tells us "broken" or "all fine". Why? Because the build server only kicks in when I decide that my change set is complete, and I push it to the server.

When those 25 seconds are a problem for, then because you are running all tests too often because you trigger them manually. Now: rather spend your energy figuring clever ways to only run the relevant tests when working on a specific problems in an efficient way. (in Java with JUnit, it is easy: I click on the current package, and go "run all tests in here)

like image 188
GhostCat Avatar answered Nov 15 '22 09:11

GhostCat