Results 1 to 7 of 7

Thread: QT Testing with Cmake

  1. #1
    Join Date
    Sep 2014
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default QT Testing with Cmake

    hi
    i m trying to make a simple unit test to run but i m having troubles adding tests and running them.
    first i configure and generate using cmake and then rebuild it with visual studio.
    now the error i m getting is


    Generating Test/moc_TestQString.cxx
    3> Building Custom Rule C:/Users/Balraj/Desktop/Workspace/Sample/CMakeLists.txt
    3> CMake does not need to re-run because C:\Users\Balraj\Desktop\Workspace\Sample_build1\CM akeFiles\generate.stamp is up-to-date.
    3> simpleplugin.cpp
    3> TestQString.cpp
    3> moc_TestQString.cxx
    3>Test\moc_TestQString.cxx(11): fatal error C1189: #error : "The header file 'TestQString.cpp' doesn't include <QObject>."
    5>------ Skipped Rebuild All: Project: ALL_BUILD, Configuration: Debug x64 ------
    5>Project not selected to build for this solution configuration
    ========== Rebuild All: 1 succeeded, 1 failed, 3 skipped ==========


    so below is the top level cmakelist

    cmake_minimum_required(VERSION 2.6)
    project(SIMPLE)
    if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
    find_path(WORKSPACE_CMAKE_ROOT Workspace_CMake_Root.txt
    HINTS ${CMAKE_SOURCE_DIR} ENV WORKSPACE_CMAKE_ROOT
    DOC "C:/Program Files/csiro.au/CSIRO-CMIS-CFD/workspace/bin")
    if (NOT WORKSPACE_CMAKE_ROOT)
    message(FATAL_ERROR "Please set WORKSPACE_CMAKE_ROOT to the directory containing the file Workspace_CMake_Root.txt")
    endif()
    include( ${WORKSPACE_CMAKE_ROOT}/CMakeLists.txt NO_POLICY_SCOPE )
    endif()

    include_directories(${SIMPLE_SOURCE_DIR})
    if (NOT ${SIMPLE_SOURCE_DIR} STREQUAL ${SIMPLE_BINARY_DIR})
    include_directories(${SIMPLE_BINARY_DIR})
    endif()


    set(SIMPLE_PLUGIN_VERSION 0.1.0)
    string(REGEX MATCH "^[0-9]+" SIMPLE_PLUGIN_SOVERSION ${SIMPLE_PLUGIN_VERSION})

    find_package(Qt4 COMPONENTS QtCore QtGui)
    include(${QT_USE_FILE})


    set(HEADERS
    ${SIMPLE_SOURCE_DIR}/simpleplugin_api.h
    ${SIMPLE_SOURCE_DIR}/simpleplugin.h
    )

    set(INSTALL_HEADERS
    ${SIMPLE_SOURCE_DIR}/simpleplugin_api.h
    ${SIMPLE_SOURCE_DIR}/simpleplugin.h
    )

    set(MOC_HEADERS ${SIMPLE_SOURCE_DIR}/Test/TestQString.cpp
    )

    set(SOURCES
    ${SIMPLE_SOURCE_DIR}/simpleplugin.cpp
    ${SIMPLE_SOURCE_DIR}/Test/TestQString.cpp
    )
    ENABLE_TESTING()

    # The next line is used by the simple application generator wizard
    # add_subdirectory(${WORKFLOWVALIDATION_SOURCE_DIR}/Application)

    #Add the test directory

    add_subdirectory(${SIMPLE_SOURCE_DIR}/Test)

    # The below line can be used to import sub-directories
    #include( ${WORKFLOWVALIDATION_SOURCE_DIR}/SubDir/CMakeLists.txt )

    include( ${SIMPLE_SOURCE_DIR}/Test/CMakeLists.txt )

    qt4_wrap_cpp(MOC_SOURCES ${MOC_HEADERS})

    add_definitions(-DSIMPLE_PLUGIN_VERSION=${SIMPLE_PLUGIN_VERSION})

    add_library(simpleplugin ${SOURCES} ${HEADERS} ${MOC_SOURCES})

    target_link_libraries(simpleplugin workspace ${QT_LIBRARIES})

    set_target_properties(simpleplugin PROPERTIES
    DEFINE_SYMBOL CSIRO_EXPORT
    VERSION ${SIMPLE_PLUGIN_VERSION}
    SOVERSION ${SIMPLE_PLUGIN_SOVERSION}
    )

    setTargetOutputDirectory(simpleplugin ${CSIRO_INSTALL_AREA}/lib/Plugins)
    configure_file(pkg-simpleplugin.cmake ${CSIRO_INSTALL_AREA}/cmake/Exports/pkg-simpleplugin.cmake @ONLY)

    # Copy our install headers into the install directory so that others can build against our plugin.
    foreach(inFile ${INSTALL_HEADERS})
    string(REGEX REPLACE "(${SIMPLE_SOURCE_DIR}/)(.*)" "${CSIRO_INSTALL_AREA}/include/simple/\\2" outFile "${inFile}")
    configure_file(${inFile} ${outFile} COPYONLY)
    endforeach(inFile)


    plus the other cmakelist which is in test directory

    find_package(WorkspaceTest)
    add_workspace_test(TestQString SCAN_MOC LIBRARIES simpleplugin workspace QTTEST)

    Now the unit test file TestQString is

    #include <QtTest/QtTest>
    #include <QObject>
    class TestQString: public QObject
    {
    Q_OBJECT
    private slots:
    void toUpper();
    };

    void TestQString::toUpper()
    {
    QString str = "Hello";
    QCOMPARE(str.toUpper(), QString("HELLO"));
    }

    QTEST_MAIN(TestQString)
    //#include "TestQString.moc" as (qt_wrap_cpp was used in the top level cmakelist not qt_automac)

    Lastly the generated moc_TestQString.cxx is

    /************************************************** **************************
    ** Meta object code from reading C++ file 'TestQString.cpp'
    **
    ** Created: Mon Sep 29 18:34:06 2014
    ** by: The Qt Meta Object Compiler version 62 (Qt 4.7.3)
    **
    ** WARNING! All changes made in this file will be lost!
    ************************************************** ***************************/

    #if !defined(Q_MOC_OUTPUT_REVISION)
    #error "The header file 'TestQString.cpp' doesn't include <QObject>."
    #elif Q_MOC_OUTPUT_REVISION != 62
    #error "This file was generated using the moc from 4.7.3. It"
    #error "cannot be used with the include files from this version of Qt."
    #error "(The moc has changed too much.)"
    #endif

    QT_BEGIN_MOC_NAMESPACE
    static const uint qt_meta_data_TestQString[] = {

    // content:
    5, // revision
    0, // classname
    0, 0, // classinfo
    1, 14, // methods
    0, 0, // properties
    0, 0, // enums/sets
    0, 0, // constructors
    0, // flags
    0, // signalCount

    // slots: signature, parameters, type, tag, flags
    13, 12, 12, 12, 0x08,

    0 // eod
    };

    static const char qt_meta_stringdata_TestQString[] = {
    "TestQString\0\0toUpper()\0"
    };

    const QMetaObject TestQString::staticMetaObject = {
    { &QObject::staticMetaObject, qt_meta_stringdata_TestQString,
    qt_meta_data_TestQString, 0 }
    };

    #ifdef Q_NO_DATA_RELOCATION
    const QMetaObject &TestQString::getStaticMetaObject() { return staticMetaObject; }
    #endif //Q_NO_DATA_RELOCATION

    const QMetaObject *TestQString::metaObject() const
    {
    return QObject::d_ptr->metaObject ? QObject::d_ptr->metaObject : &staticMetaObject;
    }

    void *TestQString::qt_metacast(const char *_clname)
    {
    if (!_clname) return 0;
    if (!strcmp(_clname, qt_meta_stringdata_TestQString))
    return static_cast<void*>(const_cast< TestQString*>(this));
    return QObject::qt_metacast(_clname);
    }

    int TestQString::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
    {
    _id = QObject::qt_metacall(_c, _id, _a);
    if (_id < 0)
    return _id;
    if (_c == QMetaObject::InvokeMetaMethod) {
    switch (_id) {
    case 0: toUpper(); break;
    default: ;
    }
    _id -= 1;
    }
    return _id;
    }
    QT_END_MOC_NAMESPACE

    Now i dont know what i m missing is it adding the tests or linking the tests

    I apologise if the problem description is very big but i thought to find the probelm i need to put everything there.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT Testing with Cmake

    So you are not using automoc but do not include the moc file, at which you have a comment that says this is needed because you don't use automoc?

    Any specific reason you are not using automoc?

    Cheers,
    _

  3. #3
    Join Date
    Sep 2014
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT Testing with Cmake

    because i dont have header files for test files cpp and also i get this cmakelist generated which has by default qt_wrap_cpp and not qt_automoc so i wanna stick to the default as its not feasible changing it everytime. plus i tried automoc it was complaning about header files and so guess i decided to stick with it

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT Testing with Cmake

    Well automoc has the advantage of being able to deal with Q_OBJECT declarations in both headers and sources.

    But well, if you really want to go with the more complicated approach, make sure you include the moc file in the source for header-less declarations (as the comment suggests) or create a header.

    Cheers,
    _

  5. #5
    Join Date
    Sep 2014
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT Testing with Cmake

    ok well i agree its bit complicated
    but wht do u mean by
    make sure you include the moc file in the source for header-less declarations (as the comment suggests) or create a header.
    according to my knowledge if we use automoc then we include .moc file in the source
    if we use qt_wrap_cpp then we do not include anything in the source file
    plus do you see any error in my add_test

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT Testing with Cmake

    There might have been a change in how automoc works, but maybe that was related to cmake+Qt5.

    Hmm, I looked at your content again and it looks like you are trying to add your test's moc file to a library?
    Shouldn't the test be an exectuable?

    Cheers,
    _

  7. #7
    Join Date
    Sep 2014
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT Testing with Cmake

    hi
    i fixed the problem
    turns out i was not ticking the test option in cmake but with few changes in cmakelist.
    Thanks for your contribution.

Similar Threads

  1. Testing Qt-Software with a GUI Testing Tool
    By nightghost in forum Qt Programming
    Replies: 8
    Last Post: 22nd February 2012, 07:43
  2. using QT unit testing with CMake
    By carpevitam in forum General Discussion
    Replies: 2
    Last Post: 3rd January 2011, 00:20
  3. Cmake
    By R.CH in forum Qt Programming
    Replies: 1
    Last Post: 7th October 2010, 17:06
  4. using moc with cmake
    By Isaac in forum Newbie
    Replies: 2
    Last Post: 29th May 2008, 03:10
  5. GUI Testing
    By larry104 in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2007, 13:46

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.