Writing Tests for Existing Code

Suppose one had a relatively large program (say 900k SLOC in C#), all commented/documented thoroughly, well organized and working well. The entire code base was written by a single senior developer who no longer with the company. All the code is testable as is and IoC is used throughout–except for some strange reason they did not write any unit tests. Now, your company wants to branch the code and wants unit tests added to detect when changes break the core functionality.

  • Is adding tests a good idea?
  • If so, how would one even start on something like this?

EDIT

OK, so I had not expected answers making good arguments for opposite conclusions. The issue may be out of my hands anyway. I’ve read through the “duplicate questions” as well and the general consensus is that “writing tests is good”…yeah, but not too helpful in this particular case.

I don’t think I am alone here in contemplating writing tests for a legacy system. I’m going to keep metrics on how much time is spent and how many times the new tests catch problems (and how many times they don’t). I’ll come back and update this a year or so from now with my results.

CONCLUSION

So it turns out that it is basically impossible to just add unit test to existing code with any semblance of orthodoxy. Once the code is working you obviously cannot red-light/green-light your tests, it usually not clear which behaviors are important to test, not clear where to begin and certainly not clear when you are finished. Really even asking this question misses the main point of writing tests in the first place. In the majority of cases I found it actually easier to re-write the code using TDD than to decipher the intended functions and retroactively add in unit tests. When fixing a problem or adding a new feature it is a different story, and I believe that this is the time to add unit tests (as some pointed out below). Eventually most code gets rewritten, often sooner than you’d expect–taking this approach I’ve been able to add test coverage to a surprisingly large chunk of the existing codebase.

12

While tests are a good idea, the intention was for the original coder to build them as he was building the application to capture his knowledge of how the code is supposed to work and what may break, which would have then been transferred to you.

In taking this approach, there is a high probability that you will be writing the tests that are least likely to break, and miss most of the edge cases that would have been discovered while building the application.

The problem is that most of the value will come from those ‘gotchas’ and less obvious situations. Without those tests, the test suite loses virtually all of its effectiveness. In addition, the company will have a false sense of security around their application, as it will not be significantly more regression proof.

Typically the way to handle this type of codebase is to write tests for new code and for the refactoring of old code until the legacy codebase is entirely refactored.

Also see.

8

Yes, adding tests is definitely a good idea.

You say that it’s well documented, and that puts you in a good position. Try creating tests using that documentation as a guide, focussing on parts of the system that are either critical or subject to frequent change.

Initially, the sheer size of the codebase will probably seem overwhelming compared to the tiny amount of tests, but there’s no big-bang approach, and making a start somewhere is more important than agonising about what the best approach will be.

I’d recommend Michael Feathers’s book, Working Effectively with Legacy Code, for some good, detailed advice.

3

Not all unit tests have equal benefit. The benefit of a unit test comes when it fails. The less likely it is to fail, the less beneficial it is. New or recently changed code is more likely to contain bugs than rarely changed code that is well tested in production. Therefore, unit tests on new or recently changed code are more likely to be more beneficial.

Not all unit tests have equal cost. It’s much easier to unit test trivial code you designed yourself today than complex code someone else designed a long time ago. Also, testing during development usually saves development time. On legacy code that cost savings is no longer available.

In an ideal world, you’d have all the time you need to unit test legacy code, but in the real world, at some point it stands to reason that the costs of adding unit tests to legacy code will outweigh the benefits. The trick is to identify that point. Your version control can help by showing you the most recently changed and most frequently changed code, and you can start by putting those under unit test. Also, when you make changes going forward, put those changes and closely related code under unit test.

Following that method, eventually you will have pretty good coverage in the most beneficial areas. If you instead spend months getting unit tests in place before resuming revenue-generating activities, that might be a desirable software maintenance decision, but it’s a lousy business decision.

1

Is adding tests a good idea?

Absolutely, though I find it a little hard to believe that the code is clean and working well and using modern techniques and simply has no unit tests. Are you sure they’re not sitting in a separate solution?

Anyways, if you’re going to extend/maintain the code then true unit tests are invaluable to that process.

If so, how would one even start on something like this?

One step at a time. If you’re unfamiliar with unit testing, then learn a bit. Once your comfortable with the concepts, pick one little section of the code and write tests for it. Then the next, and the next. Code coverage can help you find spots you’ve missed.

It’s probably best to pick dangerous/risky/vital things to test first, but you might be more effective testing something straight-forward to get into a groove first – especially if you/the team isn’t used to the codebase and/or unit testing.

3

Yes, having tests is a good idea. They will help document the existing codebase works as intended and catch any unexpected behaviour. Even if the tests initially fail, let them, and then refactor the code later so that they pass and behave as intended.

Start writing tests for smaller classes (ones that have no dependencies and are relatively simple) and move on to larger classes (ones that have dependencies and are more complex). It will take a long time, but be patient and persistent so that you can eventually cover the codebase as much as possible.

5

I often have ran into this situation, inherit a large code base without adequate or no test coverage, and now I am responsible for adding functionality, fixing bugs, etc.

My advice is to make sure and test what you add, and if you fix bugs or alter use cases in the current code, author tests then. If you have to touch something, write tests at that point.

When this breaks down is when the existing code isn’t structured well for unit testing, so you spend a lot of time refactoring so you can add tests for minor changes.

OK, I’m going to give the contrary opinion….

Adding tests to an existing, working system is going to alter that system, unless that is, the system is all written with mocking in mind from the start. I doubt it, though it’s quite possible it has good separation of all components with easily definable boundaries that you can slip your mock interfaces into. But if it isn’t, then you are going to have to make what are quite significant (relatively speaking) changes that may well break things. In the best case, you’re going to spend a load of time writing these tests, time that could be better spent writing a load of detailed design documents, impact analysis documents or solution configuration documents instead. After all, that’s the work your boss wants done more than unit tests. Isn’t it?

Anyway, I would not add any unit tests whatsoever.

I would concentrate on external, automated testing tools that will give you a reasonable coverage without changing a thing. Then, when you come to make modifications… that’s when you can start adding unit tests inside the codebase.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật