Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Unit Tests, mocking objects

I'm currently looking at some unit test librarys in C++ and have some questions:

  1. there seem to be no mocking facility in boost.test but I can hardly think of doing unit tests without creating mock objects/functions. How would you do that in boost.test, are you doing it manually (how? I mean, there are several ways I can think of, none of these seem nice) or are you simply doing without mock objects?

  2. googletest and googlemock looks like nice libraries with mockingsupport however, it requires every object that shall be mocked to be virtual. I don't really like this, it is not that I'm worrying about the performance (I could define a macro to get it out of production code anyway) but I find this very intrusive. I wonder if there's another solution which does not require that much change to the existing code? (love clojure there)

like image 235
DaVinci Avatar asked Nov 30 '10 00:11

DaVinci


People also ask

What can be mocked for unit testing?

What is mocking? Mocking is a process used in unit testing when the unit being tested has external dependencies. The purpose of mocking is to isolate and focus on the code being tested and not on the behavior or state of external dependencies.

Is mocking necessary in unit testing?

A unit test tests one unit (method or class) and uses other units only as much as necessary to achieve that goal. Mocking may be necessary, but it is not the point of the test. An integration test tests the interaction between different actual units.

What are two reasons to use mock objects in unit tests?

Typically you write a mock object if: The real object is too complex to incorporate it in a unit testing (For example a networking communication, you can have a mock object that simulate been the other peer) The result of your object is non deterministic. The real object is not yet available.


1 Answers

  1. Boost::Test does not have a mocking framework or library. If you want mocks, you have to do it yourself, or use something like GMock. Of course, you could use google mock with Boost::Test without problems.
  2. How else would you expect something to be mockable? That's how it works in every other programming language! (Okay, not with duck typing, but that carries more overhead than virtual methods) If you're concerned about performance:

    1. Implement everything in terms of virtuals as specified in the general google mock docs.
    2. Profile your code for places where that's not sufficient
    3. Replace those profiled sections (or rather, the segment of your code which indicates performance is a problem) with high-perf dependency injection instead.
    4. Don't replace everything with high-perf DI, because that would send compile times through the roof.

    In all seriousness though, I don't think the virtual calls are going to make huge differences in performance. The one case where virtuals are bad are where they're located inside of inner loops (such as in the iostream library where they're called possibly for every character of input or output), and even then only in performance sensitive code.

EDIT: I missed the very important word not in the above question #2 -- that you're not worried about performance. If that's the case then my answer is you're effectively screwed. A plain function or method call in C++ generates a plain method call, and there's no opprotunity for you to change where that call points. In most cases this doesn't require too much code change, because correct C++ code uses references wherever possible, which won't need to be modified despite the fact that virtuals are being used. You will have to watch out however for anyone using value semantics, because they will be subject to the slicing problem.

like image 182
Billy ONeal Avatar answered Sep 30 '22 04:09

Billy ONeal