DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
What's in store for DevOps in 2023? Find out today at 11 am ET in our "DZone 2023 Preview: DevOps Edition!"
Last chance to join
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Making Code Testable

Making Code Testable

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

David Bernstein user avatar by
David Bernstein
CORE ·
Oct. 12, 18 · Opinion
Like (4)
Save
Tweet
Share
8.23K Views

Join the DZone community and get the full member experience.

Join For Free

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.

unit test

Published at DZone with permission of David Bernstein, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Getting Started With JMS-ActiveMQ: Explained in a Simple Way
  • How to Configure AWS Glue Job Using Python-Based AWS CDK
  • Distributed Stateful Edge Platforms
  • Architecture and Code Design, Pt. 2: Polyglot Persistence Insights To Use Today and in the Upcoming Years

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: