Unit Test code generation

We have a project that was written over a period of 2 years, but has a poorly designed architecture and not unit tests.

The software works well, but we’re at the point where we want to refactor some core modules.

The budget is also limited so we can not hire a sufficient number of developers to write unit tests.

Would generating unit test code automatically to cover (as an example) integration scenarios via some too be a viable strategy? An assumpion is that considering the system works fine right now, and the its output can be converted into XML data for unit testing?

This approach gives us a possibility of quickly starting to refactor existing code and receieve immediate feedback if some core functionality is broken because of those changes.

2

Well, I’ve done that with a hierarchical tree data structure I wrote. The data structure parses an input data set and creates a tree based on that data set and its defined data relationships.

I created trees using various input data sets that I knew would adequately cover the various cases (there are about sixty tests), serialized each tree to XML, and used those serialized strings as expected results for the unit tests, when I was satisfied with the XML output each tree produced.

Did it work? Yes, pretty well, in fact, and it took a fraction of the time it would have taken painstakingly writing individual tests by hand.

Are there disadvantages?

  1. The tests are fairly brittle (they break when any change is made to the code).
  2. It’s not always clear what exactly is being tested in each test, since there is no mapping of tests to individual requirements.
  3. If one test breaks, several others also tend to break.
  4. Diagnosing a failed test requires taking the expected and actual test results, pasting them into a text editor, and running a diff to see what changed.

How are you going to generate matching input and output? On one hand you have the output produced by the code being tested. On the other hand you have — what? The output of a different implementation of the same algorithm? That may make sense if there is a trivial but slow algorithm that can be used to test a more complex faster algorithm. Or are you going to eyeball the expected output and just test against undesired changes? It is very easy to accept incorrect output in that case.

In my experience generated tests are as valuable as the labor that goes into them.

I never heard about unit test generation. If it was possible, it would be used by everyone. If something exists, I’m sure that it can’t give you the guarantee you that your code is properly covered.

I think that the best approach for this kind of legacy code is to write test for any new feature and any new refactoring. The first steps will be quite dangerous for the application stability but you will be soon the owner of a relatively large set of tests.

This way could be really efficient because, in a first time, you will be in front of the worst parts of the code (for necessary refactoring) and the core features (because they will be impacted by any evolution). Like that, the most critic sections of your application will be covered quickly.

You will not achieve a complete covering but if you never need to go back into an old piece of code it probably means that this code has a weak granularity with the rest.

What do you use to stimulate the unit test generator?

Consider:

int foo(int x, int y)
{
   return (x + y);
}

Your unit test generator can certainly generate code to test the addition, but it has no way of knowing that what you really wanted was a multiplication. I saw something like that bite a co-worker of mine really badly several years ago. We were using Ada, doing operator overloading to impose dimensional analysis on our arithmetic. He cut-and-pasted the rename for “+” four times, and changed the operator on the left, but not on the right. It cost him some hair-tearing to figure out why his multiply routine was returning sums instead of products. (Doing this protects you against trying to add a velocity and a distance, which is by definition meaningless.)

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