Test Driven Development (TDD): What and Why

Bonnie Schulkin
A free video tutorial from Bonnie Schulkin
Teacher | Coder | Mediocre Bassist
4.6 instructor rating • 3 courses • 32,401 students

Lecture description

TDD stands for Test-Driven Development, which means writing tests before you write functioning code. This approach leads to code that is better organized, and also makes it possible to re-test every aspect of your code without any extra effort whenever you make code changes.

Learn more from the full course

2021 Update! React Testing with Jest and Enzyme

Improve your React, Redux, Hooks and Context Code with Test Driven Development

15:00:58 of on-demand video • Updated April 2021

  • Write unit, integration and functional tests for React, Hooks, Contex and Redux using Jest and Enzyme.
  • Know the tradeoffs for different testing approaches and when to choose which approach.
  • Plan your React app more effectively via Test Driven Development.
  • Mock methods and modules to keep your tests isolated.
English [Auto] Before we dive into our first jest an enzyme tests I just wanted to lay a little groundwork on how we're going to be doing testing in this course. We're going to be using TDD or test driven development. This means we're going to be writing tests before we write the code. The idea is that you'll write a shell version of the code enough of the function that you'll be able to call it in your tests. But the function won't really do anything. Then you'll write the test and of course the test is going to fail because the code that you've written the function doesn't actually do anything. That's the red part of the red green testing you're going to get a red or a failing test first. This is important because you want to see tests fail. It is all too easy to write tests in a way that they pass regardless of what the code is doing. So this is a really nice feature built into TDD that you always get to see your tests fail before they succeed. Once you have your failing test then you write the code that makes your test pass. That's the green part of red green. Testing let me talk a little bit more about why I like TDD if you're testing your code manually. That is if you're not doing any automated tests at all you are going to be put some time and effort into running those manual tests. And if they fail then you have to put the same time and effort into running those tests again in order to see that they've succeeded with TDD. Any changes you make. You still have those tests and you get to run them without any additional effort after changes even if you're doing automated tests. If you wait until the end of all your coding to write your tests you're not taking advantage of the fact that you've built up tests all along that get run as the code progresses with TDD you end up writing better code. This is because you have to plan before you code. You have to make sure your code is planned out in order to be able to write the tests. And this makes for better organized code and this may seem obvious but your code is more testable if you are right your code with the view of testing it then you aren't going to write any code that is completely untestable and needs to be re factored in order to make tests later your code will also have fewer bugs both because bugs will be caught sooner since you're going to be doing testing as you go along. And also because you'll have regression testing built in. In other words all of the tests that you've ever written will get run every time you make a change and finally you'll have great code coverage because testing is a part of coding now code coverage in and of itself shouldn't be your entire goal of testing. Sometimes it takes way more effort than it's worth to get that last 10 or 15 percent of coverage but it is something that you want to keep in mind. And because testing is a part of coding for TDD you generally have excellent coverage in the next lecture. Let's take a look at create react app so we can actually start running these tests.