views:

301

answers:

5

Is there a good book or online site discussing the use of the CppUnit, for a beginner?

+2  A: 

Unit-testing allows testing classes in isolation, one method at a time.

Basically a testcase creates one instance of the class being tested and the class it depends on, invoke one method and verify the method worked as expected with assertions.

A great way to achieve unit-testing is Test-Driven Development (TDD), where the unit-tests are written before the code. While this may sounds weird at first, this allows to obtain code that is testable (and tested). If the code is written first, then it might not be testable easily.

The TDD process is as follows:

RED: write a test that fails GREEN: write just enough code to make it pass REFACTOR: cleanup the code, remove duplication


I'm not sure CppUnit is the most widely used. It's a portation of jUnit - the Java framework - but it's quite heavy ; there exists simplified verisons: CppUnitLite, NanoCppUnit and also Cxxtest, Boost.Test and TUT, see this list on Wikipedia. If you're not tied to CppUnit, there is an article comparing them.

philippe
+3  A: 

When you do start to use unit-testing, don't aim for 100% coverage - that way lies madness.

Look to cover all the major methods within the class (the low hanging fruit).

When I started unit testing (via UnitTest++) I went for total coverage, making methods that should of been private protected and then using wrapper classes to be able to test them. Seam functions so I could test classes that were closer to the metal.

A good book on unit testing is Working Effectively with Legacy Code which despite it's name talks about green fields development too, is well written and has good examples to boot.

Update: Also don't worry too much about covering every single possibility at the start when writing the tests. You will miss some stuff. I once had a test that passed but the live code would crash in that function. Running the inputs through the test showed what was wrong and allowed me to rewrite the function so it passed all tests. The original test had held strong for about 7 or 8 months before a customer (unfortunately for me) discovered the bug.

graham.reeds
the_mandrill
I have to say the section on levelisation is probably the most boring section (and largest) of the book, which I have to admit is very good. It probably has something to do with it being his dissertation for his doctorate.
graham.reeds
+1  A: 

Test Driven Development by Example: http://www.amazon.com/Driven-Development-Example-Addison-Wesley-Signature/dp/0321146530

Another good book that I own too.
graham.reeds
A: 

The book I turned to for "inspiration" was Working Effectively with Legacy Code by Michael Feathers. He's the author of CppUnit, and wrote this book as a way to help add unit testing to a big legacy project. If you're just starting on a new project, it's not likely to help you much, but if you're trying to get unit tests into an old project, he has a lot of good ideas.

The real problem with trying to refactor an existing project is getting the go ahead from your management. To them, if there's no perceived benefit (no new features, risk of changing code) they may think it's not worth the expense. You have to work hard to sell them on the real benefits (improved quality, ease of adding new code, etc.,) and in the end they may decide it's not worth it.

I've just picked up xUnit Test Patterns by Gerard Meszaros. It actually arrived yesterday so I've only read through the first few chapters, and can't fully recommend it one way or another yet. But so far it has provided sound advice on the author's philosophy of automated unit testing, and that may be the sort of thing you're looking for.

John Deters