Mike Kasberg

Husband. Father. Software engineer. Ubuntu Linux user.

Image for Testing is Hard

Testing is Hard

28 Jan 2017

One of my friends and colleagues writes for the Project Management for Developers blog. He recently wrote an article about the importance of testing. I want to respond to his blog post and provide some additional insight into testing from a developer perspective.

I’m going to talk mainly about unit tests here because I believe unit tests are the foundation and most important part of any test suite. But most of the ideas apply equally well to other types of tests.

Time & Coverage

Somehow, it’s always easy to find an excuse not to write tests. You’re spending too much time on tests. That component isn’t testable. You tested it manually right? We need to get this out. It’s just too hard to test. The code’s too old. It’s not worth it. If you’re a developer, you’ve heard pretty much all these excuses at one point or another.

So here’s my advice: Just write the tests! Wherever you think you don’t need tests, that’s where the bug will be. Every time. It’s almost always worth it to have tests. And as you gain more experience, you’ll find testing to be quicker and easier than you thought it was. Tests catch your bugs when you’re writing code, and they prevent you from breaking the code when you add a feature a year or two later.

Now that I’ve convinced you how important tests are, I’m going to backpedal a little bit. You’re not likely to ever reach 100% coverage. In fact, 90% or even 80% will be really hard to hit. So be smart about what you test. Test the components of your code that have the most logic and are most likely to break. And write a test whenever you fix a bug.

Testing Patterns

I mentioned that as you gain more experience writing tests, it becomes quicker and easier. It’s true, and one of the main reasons for this is that you start to recognize patterns in your code. When you’re working with old code that doesn’t have unit tests, you’ll initially find it hard to test. You need to find what Michael Feathers calls seams. Places in your code where you can inject a mock or a stub and run some tests.

As you start to look for these seams in older code, you also start adding them to new code as you’re designing it. You don’t want to have to refactor your new code just to test it later on, so you make it easy to test from the beginning. You’ll start thinking about things like dependency injection when you’re writing your constructors. Test driven development forces you to write code that’s testable. But even if you’re not doing strict test driven development, your code will be easier to test if you think about how you’ll test it when you write it. This means less time spent testing, better test coverage, and more reliable code.

Think About your Client

No matter what type of application you’re working with, well-written tests provide a huge amount of value that is not immediately apparent. When you write tests, you’re making an investment in the quality (especially long-term) of your application. Your clients are always going to want their code sooner, and tests may seem like a bad trade-off when it delays this week’s release a couple of days. This is why it’s important to explain to your clients the value that tests provide. In the long run, you’re better off having tests, and it may even increase your team’s development speed down the road.

In Summary

As a software developer, your job is to deliver working code to your client. Tests are one of the tools that enable you do do this. If you’re delivering code that doesn’t have good test coverage, you’re much more likely to be delivering buggy code. Experienced developers know this inherently because they find bugs pretty much every time they write tests. As a professional software developer, it’s part of your job to understand this, to help your client make informed decisions, and to deliver well-tested and bug-free software.

About the Author

Mike Kasberg

👋 Hi, I'm Mike! I'm a husband, I'm a father, and I'm a senior software engineer at Strava. I use Ubuntu Linux daily at work and at home. And I enjoy writing about Linux, open source, programming, 3D printing, tech, and other random topics.

Share!

Sharing my blog posts is a great way to support me and help my blog grow!

I run this blog in my spare time, without any ads. There's no need to pay to access any of the content on this site, but if you find my content useful and would like to show your support, this is a small gesture to let me know what you like and encourage me to write more great content!