Jigish Chawda
Jigish Chawda

Reputation: 2178

TDD approach for developed source code

I have been given a source code. I have to implement TDD approach to it.

How ever the general approach for TDD is:

  1. Write the test.
  2. Fail it.
  3. Write the code.
  4. Pass the test.
  5. Refactor, if necessary.

    How should I add tests to such an existing source code?
    Suggestions for an ad-hoc approach for such a case, is welcome.
    Thanks

Upvotes: 1

Views: 150

Answers (2)

guillaume31
guillaume31

Reputation: 14080

You could treat that code base as third party code and write some learning tests for it. Learning tests will allow you to discover the code while building yourself a nice test suite for further developments should you have to modify the code later.

If the legacy code is small enough, you could do that until code coverage gets near a comfortable 90 or 100%.

Upvotes: 0

user23743
user23743

Reputation:

Don't forget step 0: understand the requirements. What you can do is to discover the requirements, then write the test that demonstrates whether the requirement is satisfied. If it will pass, then great. If it doesn't, you've found a bug. Either way, you've added a regression test.

What you can't do is implement TDD (or any development practice) for code that's already been written: that boat has sailed. What you can do is enable future development on the codebase to benefit from test-driven development practices.

Upvotes: 1

Related Questions