As well as refactoring existing unit tests, I’ve also recently created some tests from scratch. I realised that, while I have gone on at length about testing on this blog, including the ways in which I think tests can be well- or poorly-written, I haven’t talked about the process of writing them. In case it’s … Continue reading Creating unit tests from scratch
Category: Unit Testing
Panning for meaning in unit tests
I recently made a code change, and also made the corresponding changes to the unit tests. Once that was sorted and tided away in a commit, I spent another commit refactoring the unit tests. As I was refactoring, I realised that the motivation behind the refactoring, i.e. what was influencing its direction, was a desire … Continue reading Panning for meaning in unit tests
Using User Experience Techniques to Introduce Mutation Testing
Someone posted a question in a Ministry of Testing Slack channel about introducing mutation testing at work. My answer is below, as someone suggested I turn it into a blog post. I've edited it slightly to make it stand on its own rather than as a response to a particular person's question. It's basically a … Continue reading Using User Experience Techniques to Introduce Mutation Testing
Testing your view models
In this article I’m going to talk about view models and similar behaviour-free containers of data. I’ll go over why you might want one, and then talk about testing them (yes, really). I use the name view model to make it clear that I'm not talking about models from e.g. machine learning. What is a … Continue reading Testing your view models
Your tests are code too
Imagine if someone submitted some production code for review, which had lots of magic numbers and magic text, lots of repetition, long methods etc. Would you say that code was OK? How much of your unit or integration test code is like that? Seeing the wood for the trees Part of the problem with test … Continue reading Your tests are code too
Tests as sheep dogs for your code
Tests can act as constraints on your code, a bit like sheep dogs herd sheep into a pen. One sheep dog stops the sheep from straying to the left, another stops them from straying to the right, and another moves them forwards towards the pen. The sheep are free to roam where they wish, if … Continue reading Tests as sheep dogs for your code
Unit testing database queries
In this post I will try to describe an approach to unit testing database queries. It happened to be inspired by some testing I was doing of some C# code, but you could apply the same ideas to other languages. Note that if you’re unit testing stored procedures then I recommend using something like tSQLt, … Continue reading Unit testing database queries
Mutation testing
Mutation testing is a way of judging the quality of your tests, just as tests are a way of judging the quality of your code. Usually, the tests that mutation testing works with are automated unit tests. In theory it could apply to manual and/or higher-level tests like integration or system tests, but I hope … Continue reading Mutation testing
Named parameters with default values can help unit tests
Introduction I find myself using default values much more in unit tests than in production code. That is, I do it more often, and use more parameters with defaults per method. This isn't because default values are bad, but they are often particularly useful when setting data up for unit tests. It makes the tests … Continue reading Named parameters with default values can help unit tests
Single Responsibility Principle for Automated Tests
I was writing some unit tests last week for a controller in an MVC application. As part of this I had to create a view model to pass to it, and inside the controller this was used to create a model. I had previously spent a bit of time writing unit tests for the conversion … Continue reading Single Responsibility Principle for Automated Tests