Search code examples
c++qtunit-testingproject-structure

Including google tests in Qt project


I'm trying to start testing my Qt project with Google tests. I read Qt Docs, but there is only description, how to create and run tests, but not integrate. So I had some questions.

  1. Should I create subproject or create separate one? Where should I place test project?

Now I store test project this way:

project.pro
|
 --- subproject1.pri
|
 --- subproject2.pri
|
| ...
|
 --- test_project.pri
    |
     --- init_tests.cpp

Is that correct way to place tests?

  1. From where should I run this code?

::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS();

Now I run it from init_tests.cpp. Then I call init_tests from main function. It seems wrong, but I don't know another way to do this.

  1. So if I use previously described way to integrate tests, how can I remove it from deployment?

  2. Where should I place tests in git repo? I created branch for my tests, then merge it to master. Is that correct?

Thanks for answers, sorry for my broken English.


Solution

  • Answer to question 4 : that seems correct for the first implementation, but then this branch must die, you don't want to have a branch with the test. Commit made on any branches must included related Unit Test and must be pushed only when tests are green...

    Answer to question 1/2 : Your tests should be in a standalone executable that should not be shipped to your client.. Normally you have one pro file per executable target. So I would rather do :

    project.pro
    |
     --- subproject1.pri
    |
     --- subproject2.pri
    |
    | ...
    |
    test_project.pro
     --- test_project.pri
        |
         --- main.cpp
    

    With main.cpp holding your lines :

    :testing::InitGoogleTest(&argc, argv);
     return RUN_ALL_TESTS();
    

    .. suddently it becomes obvious , don't you think ?

    (and of course only test_project.pro contains include and reference to Google Test )

    Answer to question 3 : become obvious too.

    Note : for that to work it supposes that what you do in subproject1 and subproject2 is accessible, i.e that those libs are either static and included in test_project.pro or dynamic lib with exported symbols.