Prepared by – Kumaresh Chandra Baruri
Software Engineer
The main idea behind TDD is
1. Test cases for each individual functionality is created.
2. The newly created test cases are run for obvious fail at the very beginning.
3. Then the new code is developed in order to pass the test cases.
4. Make the code simple and bug free.
5. Avoid duplicate codes for single functionality.
Test Driven Development (TDD) is software development approach or
method in which test cases are developed to specify and validate what
the code will do.
Test cases in TDD are –
Requirement conditions that the developers need to test to fulfill them.
Unit Testing TDD
Refers to what you are testing. Refers to when you are testing, and test first is the
base of TDD.
Unit testing is a component of TDD. TDD may utilize different components. Like – unit
testing.
Approach to –
 Write unit tests before you write your code,
 After you write your code or
 While you write your code.
Approach to - write unit test before writing code.
TDD Traditional Testing
When a test fails, developers have made progress
because they know that they need to resolve the
problem.
With traditional testing, a successful test finds one
or more defects.
More focus is on production code that verifies
whether testing will work properly.
More focus is on test case design and then apply.
Developers achieve 100% coverage test. Every
single line of code is tested.
Not certain about code coverage.
It ensures that the system actually meets
requirements defined for it.
It ensures system is tested before delivery.
TDD approach is primarily a specification technique. It ensures that your source code is thoroughly tested at
confirmatory level.
*** The combination of both traditional testing and TDD leads to the importance of testing the system rather than
perfection of the system.
1. Add a new test to the test suite.
2. (Red) Run all the tests to ensure the new test fails.
3. (Green) Write just enough code to get that single test to pass.
4. Run all tests.
5. (Refactor) Improve the initial code while keeping the tests green
6. Repeat.
Add a test
to test suite
Run and
ensure new
test fails
Run all tests
and make
sure passed
Refactor
codes
Write codes
TDD
usually
follows
"Red-Green-Refactor"
 TDD approach is neither about “Testing” nor about
“Design”.
 TDD does not mean “write some of the tests, then
build a system that passes the tests.
 TDD does not mean “do lots of Testing.”
There are two levels of TDD –
1. Acceptance TDD (ATDD):
 Write a single acceptance test to satisfy the behavior of the system.
 Develop just enough production/functionality code to fulfill that
acceptance test.
2. Developer TDD:
 Write single developer or unit test
 Then write just enough production code to fulfill that test.
 The unit test focuses on every small functionality of the system.
 Developer TDD is simply called as TDD.
1. Create a C# Class Library (.NET Standard) project named MyMath.
2. Add a new MSTest test project.
3. Add a test method named BasicRooterTest().
4. Create a Rooter class under MyMath project.
5. Run the unit test where test is failed.
6. Improve the code of SquareRoot function in Rooter class of MyMath
Project.
7. Run the unit test again where the test is passed.
8. Extend the range of inputs
9. Run again and the errors occurred.
10. Refactor the SquareRoot code as follows -
11. Run and all tests are passed.
12. Add tests for exceptional cases -
13. Run and observe infinite looping will occur.
14. Cancel the test execution.
15. Fix the SquareRoot code by adding the following if statement at the beginning
of the method:
16. Run all the test cases and all tests will be passed.
17. Refactor the code under test
18. Run all the test cases where all will be passed
The above codes were developed by using TDD
 Reduce costs for development.
 Make refactoring and rewriting easier and faster ("make it work" with red and
green stages, then refactor "to make it right").
 Streamline project onboarding.
 Prevent bugs and coupling.
 Improve overall team collaboration.
 Increase confidence that the code works as expected.
 Improve code patterns.
 Eliminate fear of change.
1. https://docs.microsoft.com/en-us/visualstudio/test/quick-start-test-driven-development-with-test-explorer?view=vs-2022
2. https://www.guru99.com/test-driven-development.html
3. https://www.browserstack.com/guide/what-is-test-driven-development
4. https://testdriven.io/test-driven-development/
5. https://softwareengineering.stackexchange.com/questions/59928/difference-between-unit-testing-and-test-driven-development
Test Driven Development

Test Driven Development

  • 1.
    Prepared by –Kumaresh Chandra Baruri Software Engineer
  • 2.
    The main ideabehind TDD is 1. Test cases for each individual functionality is created. 2. The newly created test cases are run for obvious fail at the very beginning. 3. Then the new code is developed in order to pass the test cases. 4. Make the code simple and bug free. 5. Avoid duplicate codes for single functionality. Test Driven Development (TDD) is software development approach or method in which test cases are developed to specify and validate what the code will do.
  • 3.
    Test cases inTDD are – Requirement conditions that the developers need to test to fulfill them.
  • 4.
    Unit Testing TDD Refersto what you are testing. Refers to when you are testing, and test first is the base of TDD. Unit testing is a component of TDD. TDD may utilize different components. Like – unit testing. Approach to –  Write unit tests before you write your code,  After you write your code or  While you write your code. Approach to - write unit test before writing code.
  • 5.
    TDD Traditional Testing Whena test fails, developers have made progress because they know that they need to resolve the problem. With traditional testing, a successful test finds one or more defects. More focus is on production code that verifies whether testing will work properly. More focus is on test case design and then apply. Developers achieve 100% coverage test. Every single line of code is tested. Not certain about code coverage. It ensures that the system actually meets requirements defined for it. It ensures system is tested before delivery. TDD approach is primarily a specification technique. It ensures that your source code is thoroughly tested at confirmatory level. *** The combination of both traditional testing and TDD leads to the importance of testing the system rather than perfection of the system.
  • 6.
    1. Add anew test to the test suite. 2. (Red) Run all the tests to ensure the new test fails. 3. (Green) Write just enough code to get that single test to pass. 4. Run all tests. 5. (Refactor) Improve the initial code while keeping the tests green 6. Repeat.
  • 7.
    Add a test totest suite Run and ensure new test fails Run all tests and make sure passed Refactor codes Write codes TDD usually follows "Red-Green-Refactor"
  • 8.
     TDD approachis neither about “Testing” nor about “Design”.  TDD does not mean “write some of the tests, then build a system that passes the tests.  TDD does not mean “do lots of Testing.”
  • 9.
    There are twolevels of TDD – 1. Acceptance TDD (ATDD):  Write a single acceptance test to satisfy the behavior of the system.  Develop just enough production/functionality code to fulfill that acceptance test. 2. Developer TDD:  Write single developer or unit test  Then write just enough production code to fulfill that test.  The unit test focuses on every small functionality of the system.  Developer TDD is simply called as TDD.
  • 11.
    1. Create aC# Class Library (.NET Standard) project named MyMath. 2. Add a new MSTest test project.
  • 12.
    3. Add atest method named BasicRooterTest(). 4. Create a Rooter class under MyMath project.
  • 13.
    5. Run theunit test where test is failed.
  • 14.
    6. Improve thecode of SquareRoot function in Rooter class of MyMath Project.
  • 15.
    7. Run theunit test again where the test is passed.
  • 16.
    8. Extend therange of inputs
  • 17.
    9. Run againand the errors occurred. 10. Refactor the SquareRoot code as follows - 11. Run and all tests are passed.
  • 18.
    12. Add testsfor exceptional cases - 13. Run and observe infinite looping will occur.
  • 19.
    14. Cancel thetest execution. 15. Fix the SquareRoot code by adding the following if statement at the beginning of the method: 16. Run all the test cases and all tests will be passed.
  • 20.
    17. Refactor thecode under test
  • 21.
    18. Run allthe test cases where all will be passed The above codes were developed by using TDD
  • 22.
     Reduce costsfor development.  Make refactoring and rewriting easier and faster ("make it work" with red and green stages, then refactor "to make it right").  Streamline project onboarding.  Prevent bugs and coupling.  Improve overall team collaboration.  Increase confidence that the code works as expected.  Improve code patterns.  Eliminate fear of change.
  • 23.
    1. https://docs.microsoft.com/en-us/visualstudio/test/quick-start-test-driven-development-with-test-explorer?view=vs-2022 2. https://www.guru99.com/test-driven-development.html 3.https://www.browserstack.com/guide/what-is-test-driven-development 4. https://testdriven.io/test-driven-development/ 5. https://softwareengineering.stackexchange.com/questions/59928/difference-between-unit-testing-and-test-driven-development