Unit testing should be a fundamental component of every developer’s workflow, it should never be viewed as a necessary evil! As with most things in life, unit testing should become a habit and once you start doing it, it should be something you can’t do without.I remember my first few weeks of taken up unit testing, I was doing it all wrong, in that, I was trying to retro-fit into legacy applications or legacy code.
This is not always a pleasurable experience because you will often soon find out, the majority of code developed is never developed with testing in mind and gradually you often end up rewriting entire chunks of the code base in order to make it testable!
Unit testing, is a lot like teenage sex, everyone talks about it, but really only a few are actually doing it!
In this post, I will discuss some aspects of retrofitting unit tests to legacy applications but mostly I will highlight some of the cool features of .net core that enable developers to effortlessly include unit tests into their everyday workflow.
I’ve been a long time fan of the .net framework in general, having started developing with it back in early 2002 when it was still in Beta and only available
It took a couple of years after C# was released, that the .net developers really started taking unit testing seriously. I seem to recall, it was circa 2006/7 that it really started becoming something more & more folks in the community started banging the drums about it, and the Dark Matter Herd of developers looked up from the cubicles to take notice.
Back then it wasn’t uncommon to open solutions and find 1000 lines of code behind button click events. Web applications were often difficult to test due to the fact developers were more concerned with wrestling with
ViewState and bringing their Desktop development practices to the web.
Anything to do with testing was always something you gave to the intern, in the hope that they never ask you again for some work to do!
The release of ASP.net MVC, in my view accelerated adoption of Unit testing among the .net developer community. The Microsoft Patterns & Practices team really did do a good job of trying to spread the Gospel of Bob through the community. I started to notice more and more copies of Clean Code: A Handbook of Agile Software Craftsmanship , appearing on developers desks.
The alt.net movement , circa 2007/8 then accelerated the adoption, as more and more developers started embracing Agile principles, with more and more offices getting familiar with Post-it notes on walls, and groups of people having stand up meetings in previously under appreciated corners of the office for 15 minutes during the day.
Books like first edition of The Art of Unit Testing: with examples in C# really helped .net developers adopt radical approaches to improving the quality of code and developing more and more code with testing in mind.
Views from the promise land
Myself, like a lot of developers, particularly most of the good ones I’ve met along my path, started to move away from the .net framework and Microsoft development stack in general, particularly after the fiasco that became known to the rest of the world as the release of Windows 8.
It seemed everyone at that time was starting to get involved with the new kid on the block, Node.JS and more and more developers started using MacBooks. The ROR community was ever inceasing.
Personally, I started working more on more with Linux, particularly Ubuntu and Fedora, mostly because the president of household financial affairs wouldn’t allow me the to buy a Macbook, due to the price point conflicting with purchase of other household luxuries, like the mortgage, water, electricity and heating, I also found I could extend old laptops lifespans by formatting disks and installing Linux!
I started working more with PHP and Node and even started liking Java! I did keep working with .net but under the proviso that people had to pay me to work with Windows!.
What I like about the other programming languages and frameworks is that they all seemed to be developed with Unit Testing in mind. Many the of software development strategies and practices stemmed from running your components via Unit tests for the most part. I was surprised to find that Unit Tests were prevalent in PHP, a language which has long been known for it’s large population of cowboys!
NodeJs in particular, is great to work in from a Unit Test point of view. There is an abundance of Unit Test Frameworks to select from, in fact one could waste weeks trialing the multitude of frameworks available, with more arriving by the hour!
The .net core CLI ships with support for creating unit test projects using any of the 3 frameworks available, MsTest , nUnit3 or xUnit out of the box.
The prodigal returns
It was back in late 2016, when I learned about .net core after becoming involved in project to develop cross platform software application. Originally we were considering Node, but the bright sparkle of the shiny was too hard to ignore and I succombed to the allure!
Once I installed Microsoft .net core on my Linux box, something I never thought I would do! I can only imagine it’s the same feeling a heroine user experiences. Something so wrong, that feels just right!
The more I explored .net core, the more I realised that it had been developed with Unit Testing in mind. Aspects like Dependency Injection being baked into the framework from the start, instead of being something the developer had to configure, it’s just there. You can implement whichever container of your choice but for 80% of use cases the out of the box DI container will suffice.
All this coupled with the fact, I could generate and start new projects using the CLI and Text Editor. It felt somewhat strange developing Microsoft Applications using Sublime Text on a Linux box! I kept having the feeling that this is not supposed to be the way the world works! But it does, and I have to say I like it!
This even prompted me to download Visual Studio Code and replace Sublime as my preferred editor.
Text Editors are great! For the most part developers could spend 80% of their time in a Text Editors and the extensibility and plugins available you could convert your humble text editor into a fully fledged customised IDE, and Visual Studio code is certainly capable of this. However, all you’re doing is building an IDE, and when all is said and done, a good IDE is still the number one choice for developers. Especially one that has in built support for running Unit Tests.
I always had a Love-Hate relationship with Visual Studio. It never really seemed to strike the right balance either way! There certainly are a lot of cool features but at the same time it could be so totally annoying, and prone to crashing at exactly the wrong time!
I have always found them to be best IDE, and personally like being able to have particular IDE for your language of choice. As opposed to Visual Studio trying to be one IDE for all, and failing! But hey, thats my personal opinion!
So I for one, was one of those who were particularly excited by the release of Rider – Cross-Platform .NET IDE – I wasn’t camping outside the offices of Jetbrains for days before the release excited – never the less I was eager to try it out!
The key features for me are the Unit Test runner and the Rich set of debugging tools! It’s also great to have refactoring tools which actually work!
Probably the best benefit, is opening up the IDE without it complaining to you that Resharper is actually slowing it down!
EFen Framework Core
Even the bane of most developers lives got a refreshing facelift and makeover, and in my persoanl opinion has actually become easier to work with from a Unit test perspective.
Entity Framework Core a.k.a EF Core, packs in a few features that were somewhat missing or unworkable in previous releases, in particular the support for In-Memory test Databases.
It’s now somewhat an Ironic approach to actually use Entity Framework in your unit tests, instead of trying to abstract and obfuscate it’s use in your projects so you could actually unit test them! Another strange feeling one could only associate with illicit drug abuse!
The irony is not lost on me, the fact that I made use of Entity Framework to develop unit tests, for something that is supposed to abstract the use of Entity Framework within Projects. However, in reality the purpose of the project is not really supposed to abstract the use of Entity Framework, but it actually promotes the use of it.
In my opinion, EF Core actually exemplifies how much effort and purpose the folks at Microsoft have invested and making .net core a Unit Test friendly software development framework.
My particular Unit Test Framework of choice is xUnit – a free, open source, community-focused unit testing tool for the .NET Framework. However, in reality there are no perfect Unit test frameworks, they are all just a matter of personal choice.
.NET Core has been designed with testability in mind, the result is that creating unit tests for your applications is easier than ever before. Most of the preamble .net developers needed to get their applications ready for implementing Unit Tests are no longer required. Most of the project templates available are capable of being unit tested out of the box.
dotnet new is pre-packaged with 3 different Unit Test Projects templates including : xUnit, nUNit3 and MsTest and these are extremely easy to integrate within your projects. Especially considering ASP.NET Core supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. However, you are not restricted to using the built in DI Container and it is easier than ever to configure your own DI container, although that being said the out of the box container will suffice 80% of use cases.
Denizon's product line successfully integrate IoT, Artificial Intelligence and Blockchain technology to enable efficient, productive, secure and scalable solutions to help organisations address increasing energy demands, ecological impact and Health & Safety concerns of their staff.