TDD
Last updated
Last updated
To view lecture notes for this course, please consult the .
Each test case fails initially: This ensures that the test really works and can catch an error. Once this is shown, the underlying functionality can be implemented. This has led to the "test-driven development mantra", which is "red/green/refactor", where red means fail and green means pass. Test-driven development constantly repeats the steps of adding test cases that fail, passing them, and refactoring. Receiving the expected test results at each stage reinforces the developer's mental model of the code, boosts confidence and increases productivity.
1. Add a test 2. Run all tests and see if the new test fails 3. Write the code 4. Run tests 5. Refactor code. Repeat
Requirements for First TDD Cycle Create a function that computes the average of a range of numbers. Please go to slide xyz in
Go to file path tdd/tdd-cycle/cycle1/program.test.js
and add a failing test by calling a function that doesn't exist in program.js
Add a failing test in program.test.js using either Mocha with Chai or with Tape from our previous exercies.
Run the failing test npm run tdd:cycle1
Go to file path tdd/tdd-cycle/cycle2
.
Add the minimal requirement to make the test pass again.
(Hint) Add an empty function in program.js
and then call it with the appropriate assertion.
Run the test with npm run tdd:cycle2
Go to file path tdd/tdd-cycle/cycle3
.
Implement the average function in program.js.
Add a unit test for the average function with an array of numbers.
Use appropriate assertion to unit test the function.
Run the test with npm run tdd:cycle3
Go to the file path tdd/tdd-cycle/cyclefinal
.
Refactor the code again with possible different implementation or quit.
If refactored with newer function than add new test else add run the same test for original implementation
Run the test with npm run tdd:cycle:final