Adding Googletest To Existing CMake Project

后端 未结 1 1830
情深已故
情深已故 2021-01-05 23:45

I\'m having trouble integrating googletest into my existing project. I put together a simple project to represent my project\'s structure:

Project Structure<

相关标签:
1条回答
  • 2021-01-06 00:24

    There's very little wrong with the specimen project you've posted.

    It seems you've mistakenly assumed that:

    add_test(NAME Test COMMAND Test_TestTester)
    

    in your new_test_source/CMakeLists.txt is all it takes to get your Test_TestTester executed by make.

    In fact, as declared by the first line of the add_test documentation:

    Add a test to the project to be run by ctest(1).

    your add_test command only suffices to get Test_TestTester run when, after make, you run ctest in the build directory of the Test_TestTester sub-project.

    Furthermore, even this will only happen if you enable ctest testing for that sub-project by invoking enable_testing() in new_test_source/CMakeLists.txt, which you don't. You say you did that later:

    Using enable_testing() followed by add_test() is also failing to produce any changes.

    But that's because you still haven't done anything but create tests that you can run with ctest, and still haven't run ctest.

    So assume I've got your specimen project in my working directory (which I have), and that I've just changed new_test_source/CMakeLists.txt by changing:

    project(Test_TestTester)
    

    to:

    project(Test_TestTester)
    enable_testing()
    

    Then:

    $ mkdir build
    $ cd build
    $ cmake ..
    

    generates the build system, and:

    $ make
    Scanning dependencies of target TestTester
    [ 25%] Building CXX object existing_source/CMakeFiles/TestTester.dir/main.cpp.o
    [ 50%] Linking CXX executable TestTester
    [ 50%] Built target TestTester
    Scanning dependencies of target Test_TestTester
    [ 75%] Building CXX object new_test_source/CMakeFiles/Test_TestTester.dir/main_test.cpp.o
    [100%] Linking CXX executable Test_TestTester
    [100%] Built target Test_TestTester
    

    builds everything, and:

    # We're still in `./build`
    $ cd new_test_source/
    $ ctest
    Test project /home/imk/develop/so/scrap2/build/new_test_source
        Start 1: Test
    1/1 Test #1: Test .............................   Passed    0.00 sec
    
    100% tests passed, 0 tests failed out of 1
    
    Total Test time (real) =   0.00 sec
    

    runs your tests. The complete test log is saved at:

    $ cat ./Testing/Temporary/LastTest.log
    Start testing: Feb 12 19:23 GMT
    ----------------------------------------------------------
    1/1 Testing: Test
    1/1 Test: Test
    Command: "/home/imk/develop/so/scrap2/build/new_test_source/Test_TestTester"
    Directory: /home/imk/develop/so/scrap2/build/new_test_source
    "Test" start time: Feb 12 19:23 GMT
    Output:
    ----------------------------------------------------------
    Running main() from /home/imk/develop/so/scrap2/new_test_source/main_test.cpp
    [==========] Running 2 tests from 2 test suites.
    [----------] Global test environment set-up.
    [----------] 1 test from SampleTestCase
    [ RUN      ] SampleTestCase.TestOneIsOne
    [       OK ] SampleTestCase.TestOneIsOne (0 ms)
    [----------] 1 test from SampleTestCase (0 ms total)
    
    [----------] 1 test from ExistingCodeTestCase
    [ RUN      ] ExistingCodeTestCase.TestSample
    [       OK ] ExistingCodeTestCase.TestSample (0 ms)
    [----------] 1 test from ExistingCodeTestCase (0 ms total)
    
    [----------] Global test environment tear-down
    [==========] 2 tests from 2 test suites ran. (0 ms total)
    [  PASSED  ] 2 tests.
    <end of output>
    Test time =   0.00 sec
    ----------------------------------------------------------
    Test Passed.
    "Test" end time: Feb 12 19:23 GMT
    "Test" time elapsed: 00:00:00
    ----------------------------------------------------------
    
    End testing: Feb 12 19:23 GMT
    

    If you'd like to see all that on your console at the time when you run ctest, you can run it in verbose mode ctest -V. Or if you'd like only to see the details if the tests fail, ctest --output-on-failure.

    Everything's working as it should, and maybe you're happy with that, knowing how it works. If you still want to your tests to be run automatically by make - which is not conventional - then you'll need to add a custom post-build command to the Test_TestTester target to run ctest automatically. Just append, e.g.

    add_custom_command(TARGET Test_TestTester
                       COMMENT "Run tests"
                       POST_BUILD COMMAND ctest ARGS --output-on-failure
    )
    

    to new_test_source/CMakeLists.txt. Then the output of a clean make is:

    $ make
    Scanning dependencies of target TestTester
    [ 25%] Building CXX object existing_source/CMakeFiles/TestTester.dir/main.cpp.o
    [ 50%] Linking CXX executable TestTester
    [ 50%] Built target TestTester
    Scanning dependencies of target Test_TestTester
    [ 75%] Building CXX object new_test_source/CMakeFiles/Test_TestTester.dir/main_test.cpp.o
    [100%] Linking CXX executable Test_TestTester
    Run tests
    Test project /home/imk/develop/so/scrap2/build/new_test_source
        Start 1: Test
    1/1 Test #1: Test .............................   Passed    0.00 sec
    
    100% tests passed, 0 tests failed out of 1
    
    Total Test time (real) =   0.00 sec
    [100%] Built target Test_TestTester
    
    0 讨论(0)
提交回复
热议问题