Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Making Code Testable

DZone's Guide to

Making Code Testable

Learn about steps you can take to make sure the code you write is testable for a quality result.

· DevOps Zone ·
Free Resource

Learn how integrating security into DevOps to deliver "DevSecOps" requires changing mindsets, processes and technology.

I believe that testability is one of the key characteristics of good, maintainable software. But what do I mean by testability?

Testable code is code that's written in such a way that it is independently verifiable. It has a well-defined programmatic interface and it can be fully tested based on that interface. Testable code receives dependencies as input parameters so that during testing fake dependencies can be injected instead. Testable code is made of small and functionally independent behaviors that make up a system.

When I say code should be independently verifiable I mean programmatically. In other words, we should be able to write code that exercises the specific behaviors that we want to verify.

Testable code is well-defined code so that specific inputs creates specific outputs. This often forces us to separate out our business rules from the way we display them and/or process them. This is a good thing because business rule shouldn't be intertwined with the user interface or at the persistence layer. Pulling out business rules from UI or the persistence layer makes code more testable.

When objects obey the Single Responsibility Principle and do just one thing then testing is far more straightforward. Combinatorial explosion is one area where testing can be difficult. To get around this, reduce the number of code paths through each method. This is also known as cyclomatic complexity.

A method with no conditional logic has a cyclomatic complexity of one because there is only one path through the code. Therefore there is only one test that's required to verify the code. If a method has one conditional statement that alters the path of the code in two different ways then we would say that it has a cyclomatic complexity of two and there are two tests required to verify or validate the code.

This can grow very quickly. Two conditional statements mean four paths through the code. Three conditional statements mean eight paths through the code, four conditionals mean 16 paths. It grows exponentially.

We want to keep the number of code paths in a method small so they are straightforward to test. Unit tests should exercise every code path which means that every code path should have its own unit test. Therefore, I try to keep the cyclomatic complexity of my methods as low as I can.

It's also shown that the higher the cyclomatic complexity, the more prone a method is to have defects. That's only natural because as the complexity of a method grows, our ability to manage it diminishes.

Sometimes people ask me how do you test the user interface and by and large my answer is that you don't. If you build your user interface elements without business rules so they're dumb then you don't need to test them. Extract out the business rules from your display elements and put them in their own testable objects. Not only does that help make UIs more pluggable but it also allows us to swap out different kinds of UIs if we choose. So, we can have a web-based UI or a Java-based UI, etc.

When I'm testing code I want my test to be meaningful, of course. My test should be based on some meaningful behavior that's created in the system as a result of the thing that I'm testing.

We often use the syntax of set up, trigger, verify. The setup puts the system into a state where we can run the test. The trigger triggers the behavior that we are trying to test. The verify verifies the result against some expected results. This approach to unit testing is very powerful, clear and straightforward.

I find that when I think along these lines and build software along these lines that not only do I make my code more testable but I also make my code more focused and I write far less cruft and far more code that's about fulfilling acceptance criteria. And that makes my users happier.

Learn how enterprises are using tools to automate security in their DevOps toolchain with these DevSecOps Reference Architectures.

Topics:
devops ,code quality ,testing

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}