Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing with C/C++: Lessons, what to remember?

Unit testing with C/C++: What do you teach people who either did not do unit testing before or come from Java/Junit?

What is the single most important lesson / thing to remember/ practice from your point of view that saves a lot of time or stress (especially regarding C/C++)?

like image 976
Ansgar Avatar asked Oct 21 '08 11:10

Ansgar


People also ask

What can be used for unit testing in C?

The most scalable way to write unit tests in C is using a unit testing framework, such as: CppUTest. Unity. Google Test.

What makes code difficult for unit testing?

Tightly coupled code is extremely hard to unit test (and probably shouldn't be unit tested - it should be re-factored first), because by definition unit tests can only test a specific unit of something. All calls to databases or other components of the system should be avoided from Unit Tests because they violate this.


1 Answers

  1. Unit tests have to run automatically on every checkin (or, unit tests that are written then forgotten are not unit tests).
  2. Before fixing a bug, write a unit test to expose it (it should fail). Then fix the bug and rejoice as the test turns green.
  3. It's OK to sacrifice a bit of "beauty" of a class for easier testing (like provide public methods that should not really be public, but help your testing/mocking).
like image 122
ripper234 Avatar answered Oct 08 '22 00:10

ripper234