D
D
Dmitry2014-03-08 21:24:50
C++ / C#
Dmitry, 2014-03-08 21:24:50

How to effectively unit test in C++ projects?

Hi
I want to improve the quality of my TDD work.
To explain what I don’t like, I’ll describe how life works at the moment.
The main tools are MSVS 2010/2012 and Boost.Test. My tool project has two solution files: one for the main code and one for the unit tests. In other words, now my unit tests are in fact similar to regression tests.
It should also be noted that everything is assembled with precompiled headers for me, this nuance must be taken into account when reading the text below.
The first reason why it happened: because unit tests using the main code take a long time to compile and, as the output takes longer, then I decided to separate the tests into a separate part of the solution, knowing full well that this is not TDD when you first need a test, and then the main code.
The second reason for this situation is not fast enough switching between the main code and the test one.
The third reason is that I could not find a way to select the tests that I want to run at the moment. When choosing, I expect not only to run these tests, but also to compile only that part of the main code that is necessary for the tests to work.
I just recently got Java development and I really liked how NetBeans solves all three of these problems. Switch to test code using hotkeys; executing the entire test suite or the module that is open.
I would like experienced colleagues who managed to accustom themselves to working with TDD to share their experience and find flaws in my programming practice

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Trrrrr, 2014-03-08
@Trrrrr

We have done this:
Project and tests in one solution. The project is divided into a bunch of libs, and from them there are executables that do not have much functionality. We test only either.
Compilation almost does not slow down, although we use Googletest, it can be slower with boost.
In order to use TDD effectively, it is necessary to think over interfaces in advance, as soon as you have thought over the interface, you can already write.
Even so

class Summator
{
public:
 int Sum(int a, int b) { throw std::expcetion("not implemented");}
}

Before its implementation, it is quite clear which test needs to be written.
Usually such free classes do not exist.
For one task, several classes are created that depend on each other.
In order to test them separately from each other, read about mocks and use dependency injection.

O
Orekhov Alexey, 2014-03-08
@PokimonFromGamedev

In other words, now my unit tests are in fact like regression tests.

unit tests - tests that test a specific, small application module
regression tests - are designed to check the performance of the application after changes have been made.
That is, they are not connected concepts. Regression can be done with or without unit tests.
On the subject: no need to write tests for the sake of tests. This heresy (TDD) came from the affected languages ​​(JavaScript, Python, PHP) in which it is basically impossible to write large projects.
The difficulty increases very quickly. The programmer does the work of the compiler.
And to reduce the complexity, they came up with TDD, which check your code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question