[TDD] What is Test Driven Development?
- Development practice
Write Code -> Write Test
Write Test -> Write Code
TDD Process
that we want to add to the project, and then we're going to write the code that makes that test pass.
1. Write Test -> 2. Run Test (fails) -> 3. Add Feature -> 4. Run Test (passes) -> 5. Re-factor
So the TDD process works like this.
You start by writing a test to test a certain piece of functionality.
Then you run the test.
And because you haven't implemented the feature yet, the test should fail.
And then you add the feature which makes the test pass.
After you've done that, you run the test again and it should pass, and then you can go ahead and refactor
the code that you wrote to make the test pass.
And after you refactor it, you go ahead and run the test again to make sure it's still passing as expected.
Why use TDD?
- Better understating of code
- Make changes with confidence
- Reduces bugs
1) Because you're not just thinking about how to add the feature in code. You're actually thinking about how you would test that feature using code.
2) Because you're using TDD, you should have a good test coverage in your project, which means that all of your code is covered by unit tests.
3) This in turn helps you to reduce bugs in your project, and everybody likes to reduce bugs when they're working with software.