Good afternoon, to all forum participants! Can anyone advise any material on the subject indicated in the title? Examples, description ... Everything that I found in this direction was about C #.

    2 answers 2

    TDD in C ++ is possible, however, like everything else in C ++, it is not without crutches.

    The reasons are obvious - lack of reflections, manual allocation of memory, separation into headers and files with source code, etc. In essence, TDD in C ++ is the same thing as TDD, for example, in a C # project, where everything is written inside one large unsafe block and functions from any third-party untested libraries are constantly called.

    If this does not discourage desire, then the motion vector should be something like the following:

    • Set up a separate project for testing
    • Connect the unit-testing library (I used googletest in my projects - http://code.google.com/p/googletest/ )
    • Connect a library for mocking objects (oddly enough, I used googlemock - http://code.google.com/p/googlemock/ )
    • Set up separate build versions of the main project for production and for testing, while it is obvious that the original project should link to the project for testing.

    Remarks:

    • MSVS 9+ has built-in support for googletest and the result is easy to interpret.
    • In googletest, there is an excellent macro EXPECT_DEATH , which checks if the test does not "fall" in runtime, which is very good for C ++ (because sometimes this is the only way to test some C ++ code that does not use exceptions).
    • Separate versions of builds are sometimes necessary to bypass some of the crutches of the language. In my case, it was necessary to check whether the assert would be in a specific test, but, unfortunately, the project used its own assert system with its own dialog boxes. And, of course, this was not tested in any way by the EXPECT_ASSERT macro from the googletest library. It was necessary in the build for testing to replace these assertions with standard and verifiable ones.
    • Mocking in C ++ is a thankless task, because all functions must be declared as virtual in order to be replaced with appropriate mock'i. Here again, the ideology of separate builds and defaults in the style of TEST_MOCKABLE_METHOD .
    • Personally, at first, TDD in C ++ slowed down my work very much, mostly due to "overhead" and the lack of ideology support in the IDE. I do not know, it seems to me that this situation is unlikely to change much in the near future.

    What to read:

    I would start with the reference book to googletest and googlemock libraries, there are also additional references on the topic.

    • Thank you, we will see) - brightside90

    Use boost test library , in particular unit test framework .

    • For some reason, when I try to follow this link: boost.org/libs/test , to get the latest version of the library, I get a redirect to boost.org/doc/libs/1_47_0/libs/test/doc/html/index.html where I can't find the download link = (Can I not look there?) - brightside90
    • 2
      @ myasnik90 The easiest way is to download the entire boost latest version, run the bootstrap to build it (including boost::test ) and then use the compiled library in your project. If anything, the boost uses boost::test for self-testing, so good examples of use can be found there. - Costantino Rupert