Test Driven Development (TDD) and its Best Practices

Custom User Experiences



Test Driven Development, What is It?

Test driven development(TDD) is a software engineering practice in which we write test against new features before we actually write the code for them. This gives us a red light green light software and web development cycle that also provides living documentation, and allows us to release improvements and updates to our website design or software application with confidence that our application works.

Unit Testing

Unit testing and unit testing frameworks allow us to run tests on our code as we develop and design software and web applications, without having to build the entire application and waste time doing unnecessary debugging.

Unit of Work

A unit of work can be thought of as the smallest amount of work any given lines of code are doing. This could be a method/function, or it could be as small a line of conditional programming logic.

A typical unit test might might assert against the case logic in a method, verify that another function was called with the right parameters, or maybe assert that an artifact was rendered properly.

Testing Frameworks

No matter what language you are coding in, there is most likely a framework that provides the means to run unit tests. In Java we might use JUnit, as well as Jest in Javascript.

Mocking

Mocking is the practice of replacing software and application objects your code is dependent on with test purpose specific fakes. This not only simplifies and makes units of work smaller, but also decouples your code from dependencies you may not own or control.

Mocking allows us not only to verify that a method calls another with the right arguments, but also gives us the means to control what an external dependency returns. This allows us to test all the edge cases our application logic might encounter.

Integration Testing and Functional Tests

With integration tests, we make assertions on the entire running application. If we were engineering a web service, an integration test might verify that endpoints return the appropriate responses when being called with requests.

Integration and functional tests usually verify that all software dependencies and application components are wired up and working together properly.

Test Driven Development

TDD

Red Light, Green Light

The idea behind TDD is very simple, test first, code last. That is, you engineer features with failing tests.

First, you write a test that asserts that the functionality you are about to add works correctly. This gives us a failing test(red light), but we must be careful that the test fails for the right reason. Now we add the code, and make the test pass(green light).

Living Documentation

When we develop applications with TDD, our software now has living documentation without ugly and unsightly comments. Every faucet of code has a test, that with good naming conventions, tells us what the code does. This is one of the most valuable aspects of good test driven development practices. We could handle our entire codebase of to an entirely new team, and assume with some confidence that they can get up to speed in a reasonable amount of time.

Refactoring

Refactoring is the act of changing, or reorganizing our code, while retaining the same behavior. Code that has been well covered with tests, can be refactored with confidence. That is if we add functionality that breaks another part of our application, some test will fail and let us know.

Deploy Often

The practice of test driven development allows us to deploy as often as the users can stand, with confidence that our code is always in a working state. Code that has been well covered with tests, can be refactored with confidence. That is if we add functionality that breaks another part of our application, some test will fail and let us know.

We now have a test driven development life-cycle that we can relate to a traffic light:

  • Red: Write the failing test
  • Green: Write the code(Make the test pass)
  • Blue: Refactor the code

Why it Matters

Unit and integration testing allow us a sustainable way to test small units of work withing our application. Test driven development is a practice in which we write failing tests for features before we write the actual code to implement them.

TDD(test driven development) gives our code living documentation and allows us to make iterative refactoring passes on our code with confidence. Using tdd, we don't have the need for comments(which become hard to maintain as the code, and purpose of the code change over time) cluttering up our codebase.

With TDD we can deploy as often as users can tolerate, and know our code always works. To learn more about TDD, or to otherwise provide valuable feedback or suggestions, please reach out and we will be glad to get you off to a good start.

Share this Post