TDD
To view lecture notes for this course, please consult the github-pages.
Definition of TDD via Wikipedia Test-Driven Development
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.
Test-Driven Development Cycle
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
Exercise Instructions
Requirements for First TDD Cycle Create a function that computes the average of a range of numbers. Please go to slide xyz in github-pages
Test-Driven Development Cycle 1 (Add a Test / Run Tests)
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.jsAdd 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
Test-Driven Development Cycle 2 (Write the Code / Run Tests)
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
Test-Driven Development Cycle 3 (Refactor by adding implementation / Add a test / Run all Tests again)
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
Test-Driven Development Cycle Final / (Refactor code / Add a test / Run all tests again)
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
Last updated