Test-Driven Development

Get introduced to test-driven development.

Within the world of automated testing is a technique known as test-driven development ( TDD). TDD is the practice of writing a failing test for code first and then writing the code to get the test to pass.

Why TDD?

One benefit of TDD is that the tests are the first consumer of the code. The tests are using the code that has yet to be created, stating an expected result that it isn’t receiving. From there, we’ll implement the code to make it pass, satisfying the expectations of the first consumer. Another failing test can be added, which is then implemented to get the test to pass. At this point, we’ve confirmed that our new addition not only gets the second test to pass, but it also doesn’t negatively affect the first test (assuming it still passes).

Once the feature has been completed with all test cases passing, we might decide that our code is a bit sloppy and could be rewritten another way. With tests already written, we can now refactor our code (changing its form without changing its function) with confidence, verifying that our tests are still passing after the change.

Get hands-on with 1400+ tech skills courses.