Pros and Cons of Using the C assert Macro for Unit Testing in C/C++

This is a short follow-up to my recent article A Very Simple Introduction to Unit Testing. In that article I described the development of a very simple unit test for a very simple C++ class. To do this I used the C/C++ assert() macro, saying the following about it:

"…although assert wasn’t originally intended for unit testing, it makes a simple but effective tool for doing so without the need to look outside the language’s standard library."

I’d like to thank John Crickett for – in an offline conversation – pointing out that there are some things that I should have made clearer. Although – as I said – assert is very effective for unit testing without the need to look outside the C/C++ standard library, it does have its shortcomings. Let me summarise the points for and against.

Points for:

  1. There is no need to look outside the language’s standard library, so programmers can start unit testing without the overhead of having to set anything up
  2. If an assert fails, the program stops running. Therefore, the programmer must fix the problem before continuing. Note this is also a point against (below), but the reasoning is different

Points against:

  1. It is only available to programmers working in C and/or C++ (obviously)
  2. You can only test the debug version of your code
  3. If an assert fails, the program stops running. This makes assert unsuitable for unit tests that are run in any kind of automated mode – for example, some automated build systems run the unit tests immediately after the applicable unit of code has been built

Therefore, based on the above, I suggest the use of assert is suitable for programmers unit testing in the following situations:

  • Learning to write unit tests (see 1, under points for), either as part of a formal training course or otherwise
  • Unit testing in a development environment which has no unit testing culture (again, see 1, under points for). Programmers can develop their own unit tests without the need for support from their development environment. For example, they may not be allowed to – or even able to – install libraries other than those officially used by the development group

Thinking about it now, my original assertion (pun intended) was over zealous, in that I didn’t qualify it with the context I was thinking in at the time. This article should serve to set the record straight.

The question remains: what should be used in those situations where assert is not sufficient? I was going to summarise some of the alternatives available in C++, but it now occurs to me that such a summary would really be out of place in this article. This is because – going back to its predecessor A Very Simple Introduction to Unit Testing – I only used C++ in the first place so I could use assert, and I wanted to use assert in the first place for simplicity. That is to say, the discussion here is about the suitability of assert. Therefore, I’ll take the approach of coming back to this topic soon, and write a summary of the available unit testing libraries/frameworks for several popular programming languages. When I’ve done so, I’ll post an update here with a link to it.

Leave a Comment

Your email address will not be published. Required fields are marked *