Can unit testing be successfully added into an existing production project? If so, how and is it worth it?

前端 未结 23 2500
遇见更好的自我
遇见更好的自我 2020-12-12 09:01

I\'m strongly considering adding unit testing to an existing project that is in production. It was started 18 months ago before I could really see any benefit of TDD

23条回答
  •  忘掉有多难
    2020-12-12 09:41

    I do agree with what most everyone else has said. Adding tests to existing code is valuable. I will never disagree with that point, but I would like to add one caveat.

    Although adding tests to existing code is valuable, it does come at a cost. It comes at the cost of not building out new features. How these two things balance out depends entirely on the project, and there are a number of variables.

    • How long will it take you to put all that code under test? Days? Weeks? Months? Years?
    • Who are you writing this code for? Paying customers? A professor? An open source project?
    • What is your schedule like? Do you have hard deadlines you must meet? Do you have any deadlines at all?

    Again, let me stress, tests are valuable and you should work to put your old code under test. This is really more a matter of how you approach it. If you can afford to drop everything and put all your old code under test, do it. If that's not realistic, here's what you should do at the very least

    • Any new code you write should be completely under unit test
    • Any old code you happen to touch (bug fix, extension, etc.) should be put under unit test

    Also, this is not an all or nothing proposition. If you have a team of, say, four people, and you can meet your deadlines by putting one or two people on legacy testing duty, by all means do that.

    Edit:

    I'm aiming to write this question up later with pros and cons to try and show management that it's worth spending the man hours on moving the future development of the product to TDD.

    This is like asking "What are the pros and cons to using source control?" or "What are the pros and cons to interviewing people before hiring them?" or "What are the pros and cons to breathing?"

    Sometimes there is only one side to the argument. You need to have automated tests of some form for any project of any complexity. No, tests don't write themselves, and, yes, it will take a little extra time to get things out the door. But in the long run it will take more time and cost more money to fix bugs after the fact than write tests up front. Period. That's all there is to it.

提交回复
热议问题