Five tricks to speed up your Unit Testing today
One of the main reasons I hear for not writing tests is -"We don’t have the time!" and that it feels like it would take longer time to write code with test than without. And time is money.
I actually think I write code faster with tests than without but that might come from experience and I respect that time is a concern some people might have against Unit Testing, especially in the beginning.
I usually try to calm these concerns by asking these people to consider this time as a longterm investment. Yes, it might take you a few more minutes to complete your implementation while writing tests, but I promise you that you will make up for that time in the long run if you count in bugfixes and future improvements on that feature.
But there are a few tricks you can apply to speed up your Unit Testing today, so here are my favourite tricks:
1. Test Sessions (Playlists)
Running all your tests on every test run will eventually make your test runs slower and slower as you add more tests to your project, so what I usually do when I’m implementing a new feature is that I scope out the tests that are related to the feature I’m currently working on.
This way I only need to run these few tests, which takes just a few seconds, and once I'm done with the implementation and I’m about to send a PR to my co-workers I make sure to run all tests (while getting a cup of coffee ☕️) to make sure I haven’t broken any unexpected tests.
If you’re using ReSharper this is done by right-clicking on a test or a class and selecting "Create new session" and if you are not using ReSharper you can create a playlist in the Test Explorer in Visual Studio.
2. Run your tests in parallel
I’ve already covered this in another blog post “PARALLELISM: NUNIT VS. XUNIT” that running your tests in parallel can significantly improve the speed of your test runs. Basically this means all your tests will run "next to each other" instead of "one by one".
Read this mentioned blogposts before applying this because there are a few thing to be aware of and the configuration might look different depending on which test framework you are using.
3. Base class mocking
A big part of Unit Testing is mocking and it’s usually what takes up most of the time. If you find that you are constantly mocking the same behaviour over and over again, consider moving these mocks to a base class that you can use for all tests in that context.
But make sure you don't add all mocks to the constructor unless you know that you will be needing them for every test, instead create base methods that you can use when you need them. And make sure they are virtual, so you can override them when needed.
4. Copy & paste as much as you can!
I rarely start a new test class from an empty class. I’m lazy so I usually copy & paste a test class that I know will have similar test behaviour and mocks that I’m going to be needing for the feature I’m about to implement.
And as I’m extending my class with more tests I usually find myself copying tests from all over my test project to have something to start from. And the great thing is that the more tests you add to your project the more variety you’ll have to choose from when creating even more tests.
5. Code Templates
This is a ReSharper specific trick that I've used for years and has helped me speed up my test writing a lot.
In ReSharper there is this thing called "Code Templates" which basically means you can define complete code chunks that should be linked to a shortcut of your choice. I have many of these code templates setup related to Unit Testing, which is part of the reason I have a hard time letting ReSharper go.
Following these instructions you can setup these kind of Code Templates that I have setup for [Fact] and [Theory] tests for example:
Hope these little tricks might help some of you speed up your test writing. Happy testing friends, cheers! ❤️