Results 1 to 4 of 4

Thread: Cmake with QT Tests

  1. #1
    Join Date
    Feb 2018
    Posts
    21
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Cmake with QT Tests

    Hi.
    So I heard somewhere that testing your code is a good idea.

    In my project I am using CMake with visual studio 2015.

    I found out how to add my QT Tests to my project, using CMake, though it wasn't easy.

    My problem is that when a tests fails, it causes CTest itself to fail:


    1> Test project C:/work/foo/build/msvc2015_64
    1> Start 1: foo
    1> 1/1 Test #1: foo...............***Failed 0.05 sec
    1>
    1> 0% tests passed, 1 tests failed out of 1
    1>
    1> Total Test time (real) = 0.06 sec
    1>
    1> The following tests FAILED:
    1> 1 - foo(Failed)
    1> Errors while running CTest
    1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.Cp pCommon.targets(133,5): error MSB3073: The command "setlocal
    1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.Cp pCommon.targets(133,5): error MSB3073: "C:\Program Files\CMake\bin\ctest.exe" --force-new-ctest-process -C Debug
    1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.Cp pCommon.targets(133,5): error MSB3073: if %errorlevel% neq 0 goto :cmEnd
    1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.Cp pCommon.targets(133,5): error MSB3073: :cmEnd
    1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.Cp pCommon.targets(133,5): error MSB3073: endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
    1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.Cp pCommon.targets(133,5): error MSB3073: :cmErrorLevel
    1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.Cp pCommon.targets(133,5): error MSB3073: exit /b %1
    1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.Cp pCommon.targets(133,5): error MSB3073: :cmDone
    1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.Cp pCommon.targets(133,5): error MSB3073: if %errorlevel% neq 0 goto :VCEnd
    1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.Cp pCommon.targets(133,5): error MSB3073: :VCEnd" exited with code 8.

    If no tests fail, then there is no problem.

    The test fails:

    Qt Code:
    1. void TestSignals::testgetName() {
    2. QCOMPARE(s.getName(), "dfsdf ");
    3. }
    4.  
    5. QTEST_MAIN(TestSignals);
    To copy to clipboard, switch view to plain text mode 

    The test passes:

    Qt Code:
    1. void TestSignals::testgetName() {
    2. QCOMPARE(s.getName(), "");
    3. }
    4.  
    5. QTEST_MAIN(TestSignals);
    To copy to clipboard, switch view to plain text mode 

    Another problem is, that I don't get any diagnostics. In the documentation it states that QCOMPARE should give a more descriptive error message, but as you can see, I get nothing.

    I suppose the missing messages are caused by the fact that CTest fails. Is there a way to make CTest not fail just because a test fails?

    Best Regards
    DAVC

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,190
    Thanks
    36
    Thanked 1,489 Times in 1,441 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Cmake with QT Tests

    Quote Originally Posted by DAVC View Post
    Another problem is, that I don't get any diagnostics. In the documentation it states that QCOMPARE should give a more descriptive error message, but as you can see, I get nothing.
    You can either run ctest with the "-V" ("--verbose") option or you run the test individually.

    Once you have multiple tests it is usually nice to have ctest just run through all with "summary" output and then one can investigate any test that fails more closely.

    Cheers,
    _

  3. #3
    Join Date
    Feb 2018
    Posts
    21
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Cmake with QT Tests

    Thanks for your reply anda_skoa.

    Can you elaborate on how to invoke the verbose option for CTest in CMakeLists.txt?


    include_directories(${CMAKE_SOURCE_DIR})


    SET( TEST_LIBRARIES fileparsing Qt5::Test)

    SET( foo_SRCS foo.cpp )
    ADD_EXECUTABLE( foo ${foo_SRCS} )
    TARGET_LINK_LIBRARIES( foo ${TEST_LIBRARIES} )
    ADD_TEST( NAME foo COMMAND foo )

    set(CMAKE_CONFIGURATION_TYPES Debug;Release)

    if(WIN32)
    if($<CONFIGebug>)
    get_target_property(WIDGETDLL Qt5::Test IMPORTED_LOCATION_DEBUG)
    get_target_property(WIDGETDLL Qt5::Core IMPORTED_LOCATION_DEBUG)
    else()
    get_target_property(WIDGETDLL Qt5::Test IMPORTED_LOCATION_RELEASE)
    get_target_property(WIDGETDLL Qt5::Core IMPORTED_LOCATION_RELEASE)
    endif()

    add_custom_command(
    TARGET foo POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
    $<TARGET_FILE:Qt5::Test>
    $<TARGET_FILE:Qt5::Core>
    $<TARGET_FILE_DIR:foo>
    )
    endif()

    Best Regards
    DAVC

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,190
    Thanks
    36
    Thanked 1,489 Times in 1,441 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Cmake with QT Tests

    Quote Originally Posted by DAVC View Post
    Can you elaborate on how to invoke the verbose option for CTest in CMakeLists.txt?
    The CMakeLists.txt is for building the tests, their dependencies.

    The mention option is a commandline argument for when you run the ctest program.

    For example if you run ctest from a shell then

    $ ctest

    will run all tests and just report status (passed, failed, etc) for each one, while

    $ ctest -V

    should also show the output of each test.

    Cheers,
    _

Similar Threads

  1. Automatic tests for Qt GUI
    By qt_developer in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2014, 15:05
  2. create QApplication in a dll for uni tests
    By rajji_saini in forum Newbie
    Replies: 2
    Last Post: 16th July 2012, 20:19
  3. Unit tests can not be executed
    By Martin Drozdik in forum Newbie
    Replies: 4
    Last Post: 24th May 2012, 03:09
  4. Unit tests
    By leoalvesmachado in forum Newbie
    Replies: 1
    Last Post: 13th April 2010, 21:50
  5. how to best separate tests from source
    By Rockem in forum Qt Programming
    Replies: 0
    Last Post: 25th September 2009, 00:06

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.