First things first. What is a test isolation framework? It’s not a terribly difficult question to answer, but it’s not a trivial one either. So let’s get that out of the way, first. And then, we’ll look at some options you have in the .NET world for test isolation frameworks.
What is a Test Isolation Framework?
Test isolation is a concern on the software development side of the house. Most people understand these days that unit testing is a good idea for software developers. But unit testing can prove difficult in a vacuum, without some tools to help you along the way.
One such tool is the isolation framework. Test isolation frameworks aim specifically to aid you in partitioning (i.e. isolating) code into small, atomic units that are easy to reason about.
To make it less abstract, consider a conceptual example. Your car has a keyed ignition and an engine, and let’s say that you wanted specifically to test the ignition. That’s a pretty tall order for you, since turning your key will turn the engine on, and if that doesn’t work, you can’t really know if the problem lies in the engine or the ignition. Test isolation frameworks to the rescue! These frameworks let you isolate (the code equivalent of) the engine and just test the ignition.
Test isolation frameworks are also often called mocking frameworks, though I would argue that isolation is a more complete and better descriptor. So let’s take a look at a few of them in the .NET world, and see what they offer.
First up, an oldie but a goodie: Moq. Moq has been around quite a while, and it’s completely free and open source. It came about with the original intent of providing a free solution to allow developers not to have to home-roll their own isolation techniques, and it takes heavy advantage of Linq for configuration.
It’s also what’s known as a lightweight mocking (or isolation) tool. This means that it will have relatively good performance, but that you can use it in a relatively narrower set of contexts. Specifically, you can use it to mock virtual methods/properties/events, and methods/properties/events on interfaces. If, however, you have static members or non-virtual members, you won’t be able to use Moq for that.
This makes it well suited for newer codebases or ones designed with testability in mind (e.g. developers are practicing TDD), but you may have a harder time using it for legacy or test resistant codebases.
Next up, let’s go straight to the epicenter of the .NET ecosystem. Microsoft actually has a complete isolation framework that it offers as well: Microsoft Fakes. This started out as a research project known as Microsoft Moles, but has since evolved into a mature offering.
This tool comes straight out of the box with Visual Studio, which makes it very attractive. And it’s also really powerful. But with this power and convenience come a couple of trade-offs. First of all, setup is a little more complex and less intuitive. And, secondly, Fakes requires the highest tier of Visual Studio. So, while it’s “free” once you have that, getting to that level of service won’t be cheap.
In contrast to the powerful and heavyweight Fakes isolation, FakeItEasy is another lighter weight framework in the mold of Moq. Also like Moq, it’s an open source tool.
FakeItEasy has some really nice documentation to help you get started — more than you would see from the average open source project. It also has a really slick, fluent interface for setting up your isolation with human readable syntax, such as A.CallTo(() => customer.GetFirstName()).Returns(“Erik”).
It has a similar essential value proposition to Moq, so which of these you use, assuming they meet your requirements, will be largely a matter of your preferences.
Last up, let’s take a look at Typemock’s Isolator. This tool is in the same vein as Fakes — it’s a fully featured isolator that lets you mock anything you want and isolate all of your code.
Typemock Isolator, however, builds additional functionality on top of isolation, offering you code coverage data, a test runner, and even functionality that suggests unit tests based on your production code. On top of that, Typemock offers support across other languages, including C and C++. This is truly a full featured tool.
Typemock Isolator is an ideal tool for when you’re looking to really invest in getting up to speed with unit testing in a legacy codebase and to make unit testing a way of life for your team.
So that’s a look at the idea of test isolation frameworks and some examples. That should be enough to get you started and doing a bit of research.
I’ll close with one last bit of advice, and that’s to shop around. There’s no tried and true way to decide which of these tools will be the best for you, and all of them will most definitely help your unit testing efforts. So pick one or more of them, give them a try, and see how you like them. Whatever you choose, isolating your ignition from your engine is going to help your unit testing efforts.
This is a guest post by Erik Dietrich, founder of DaedTech LLC, programmer, architect, IT management consultant, author, and technologist.
- Announcing TestRail 6.6 with Enhanced Administration
- Announcing TestRail 6.5: New Plugins, Enhanced Integrations & Searchable Drop-downs
- TestRail Tops the Winter 2021 G2 Grid Report for Test Management
- Announcing TestRail 7.0 with Shared Test Steps and Test Case Restore