Build Qt Tests with CMake

前端 未结 2 1858
Happy的楠姐
Happy的楠姐 2021-02-01 17:47

Can anyone give me an example of some QT test code and a CMakeLists.txt that build with Cmake and ran with CTest. I can\'t seem to find any!

-Kurtis

2条回答
  •  独厮守ぢ
    2021-02-01 18:11

    Here is an example of using cmake 2.8.11 and Qt5.2. Note that cmake now supports testfiles with a .moc-include at the bottom.

    CMakeLists.txt:

    cmake_minimum_required(VERSION 2.8.11)
    project(foo)
    
    enable_testing()
    
    # Tell CMake to run moc when necessary:
    set(CMAKE_AUTOMOC ON)
    
    # As moc files are generated in the binary dir, tell CMake
    # to always look for includes there:
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    
    find_package(Qt5Test REQUIRED)
    
    add_executable(foo foo.cpp)
    add_test(foo foo)
    
    target_link_libraries(foo Qt5::Test)
    

    foo.cpp:

    #include 
    
    class Foo : public QObject {
        Q_OBJECT
    private slots:
        void t1() { QVERIFY(true); }
    };
    
    QTEST_MAIN(Foo)
    #include "foo.moc"
    

提交回复
热议问题