Tuesday, November 10, 2015

Include mock

Although recently I write mostly in (server side) JavaScript, I still have to return to my old C++ project once in a while. The other day I got notified that one of our integration tests will stop working due to changes in the database layer. It was testing a fix for a crash caused by inconsistent data coming from the db. So this integration test was trying to reproduce the situation by manipulating the data in the db. Actually reproducing such special cases with an integration test might be very difficult and unit tests are usually a better tool for this task. In the end I wanted to test some safety checks in our code, not an end to end scenario.

Still the code under test was a big and hairy lump of C++ code with many hard-wired dependencies. It certainly was not written with testability in mind. While looking around for mocking solutions in C++, I came across the section about testing from Michael Feathers' book Working Effectively with Legacy Code. He describes several approaches to mocking in C/C++, as he calls them seams. I was most impressed by the preprocessing seams. As Michael says, the preprocessor in C/C++ is kind of compensation for its stiffness compared to dynamic languages. It turns out the preprocessing seams are very powerful. You can take a C/C++ source file and compile it in a different environment thus making it do something very different.

So inspired by preprocessing seams I derived my own mocking approach in C/C++ that allows mocking of any function or class (even static, global and non-virtual) without changing the source file where these are called.

Here is the overall structure of a test file:
  1. Disable the original header that defines the dependency to be mocked using include guards
  2. Provide alternative/mock definition of that dependency
  3. #include the source file to be tested
  4. Write the tests
Let's see a simple example.
First, the header that defines the dependency that we want to mock.
Notice that this header uses include guards.


#ifndef STORE_H
#define STORE_H

class Connection;

class Store
    Store(Conection& conn);
    const char* fetch(const char* query);
    void store(const char* data);

#endif // STORE_H

Next, the code that we want to test.

#include <string.h>

#include "store.h"

int measure(Store& store, const char* query)
  const char* v = store.fetch(query);
  return strlen(v);

And now the test.
We want to mock Store. To do this, we disable the original header by defining its include guard STORE_H. Then we provide our mock implementation. Note the mock does not need to be compatible to the original class. We just need to provide the minimum so that the code under test can compile and execute. So we implement only the methods used during the test.
Then we include the code to be tested consumer.cpp so it will compile in our mocked environment.
Finally, we run our test.

#include <iostream>
#include <cassert>

using namespace std;

#define STORE_H
class Store
    const char* fetch(const char* query)
        return "ola";

#include "consumer.cpp"

int main(int argc, char *argv[])
    Store mock_store;
    assert(measure(mock_store, "query") == 3);
    cout << "OK" << endl;

With this approach all the code related to the test is in one place. You don't need to tweak any additional compiler/linker configurations. Also notice that we did not change the original code consumer.cpp, still we changed its behavior by compiling it in a mock environment.

Also described briefly this technique on SO.

No comments:

Post a Comment