A Question about Unit Testing Frameworks

Started by
3 comments, last by Xeee 20 years, 2 months ago
after reading alot about refactoring, i realized the importance of testing, and as i knew about testing, i heard about unit testing frameworks(junit, cppunit, unit ++, etc). to set up a test using any of these libraries, it takes up effort and time(you make a class derived from test_case, and override a virtual function) and i don''t see the point behind that, why not just use a simple assert() or printf() to confirm that all your tests ran successfully. something like that: assert(Add(5, 4) == 9); // Add(int, int) function test. thanks for care xee..
xee..
Advertisement
As you say, they are just frameworks. They are designed to help make testing easier, allowing you to group and run tests. You still have to write the individual tests yourself and the frameworks can often complicate matters and end up including parts of the langauge you may not want to use (e.g. exception handling in c++). For large scale projects that need to be rigorously tested they can save time and effort. On a smaller scale it is easier and often quicker to write your own tests for classes as you write them.

Assert is an invaluable tool, in each test you hould use it to test the pre and post conditions, e.g. check that all inputs are in the expected range (pointers are not null etc.) and that the output is as expected.

This article on CodeProject.com may help. It describes testing using cppunit, but also has some more general info on designing tests.
quote:Original post by Xeee
why not just use a simple assert() or printf() to confirm that all your tests ran successfully.

something like that:

assert(Add(5, 4) == 9); // Add(int, int) function test.

But that''s exactly what those frameworks do...
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
You can keep adding more and more suites and all the tests keep getting run. You can refactor the tests too! So you get rid of redundant and error prone code in your tests as well as your real code. They''re not a silver bullet and require discipline, but they do speed things up as you''re getting consistant feedback. You become aware of making progress.

If you write tests as you go along its not such a chore but if you go and retro-fit your tests it definitely is tedious. Once you''ve got them in place the fun starts but it can be difficult to maintain the discipline to get there.

I''d also recommend these articles on objectmentor.com particularly the ones on test driven development, starting at the bottom

This topic is closed to new replies.

Advertisement