Lesson Monday

We learned about the basics of Test-Driven Development in Introduction to Programming. So far, we have used the basic principles of TDD to help organize and plan our code - but we aren't actually executing any tests yet. Well, we're about to start doing that!

At this point, you will no longer need to add pseudo-coded tests in your READMEs. That was practice for writing actual tests which we can run. And that's exatly what we're about to start doing with Jest!

Before we move on to learning more about the testing workflow and using Jest, let's look at all the advantages of TDD and why it's so helpful to the development process.

TDD helps us creates a plan

By taking time to think through a program, identify behaviors, and tackle the simplest first, we're creating a game plan. We are laying out each step we need to take in detail, which leads to the next benefit.

It's easier to tackle complex problems

Even the most experienced coders can struggle with approaching complex issues. By identifying and testing the smallest possible behavior at a time, we can take baby steps towards solving bigger problems.

It prevents errors and makes bugs easier to locate

When we implement and test a single behavior at a time, we're preventing errors. Testing each behavior before moving on allows us to spot bugs as soon as they're introduced.

Without TDD, we might not spot some of these bugs until they break our application. To make matters worse, if we add more code after introducing a bug, other parts of our application will likely be reliant on our buggy code. To resolve this issue, we'd need to locate the bug, resolve it, and potentially alter all the code that depends on the buggy code. That's a lot of work.

It allows us to create projects faster

Starting a complex project can be daunting. Even if we aren't sure how to code the more difficult features of our application, we can hit the ground running by implementing smaller, identifiable behaviors. As we work, the bigger picture will come into focus.

New features are built upon reliable code

By testing behaviors as they're implemented and ensuring previous tests still pass when new behaviors are introduced, new features are always built on a foundation of reliable, tested code.

It keeps code DRY

By implementing the least amount of code to pass each test, we keep our code DRY. By approaching each behavior individually, we are likely to write more modular code. Modular code is easier to maintain, update, and debug.

It's employable

Testing is an important part of the tech industry and many tech careers rely on it - ranging from developers and QA testers to devops specialists and site reliability engineers.

Writing Coded Tests

Up to this point, we've pseudocoded our tests and then manually checked to see if our code works. This is a time-consuming and unrealistic way to test our code - and it's not what professionals do. Now we're ready to level up our TDD skills by writing automated tests for each behavior. While writing coded tests may take longer at first, we'll be able to test all of our program's behaviors with a single command.

Lesson 24 of 48
Last updated February 4, 2021