PDA

View Full Version : QT Testing with Cmake



dhillon_5230
29th September 2014, 10:24
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.

anda_skoa
29th September 2014, 11:57
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,
_

dhillon_5230
29th September 2014, 12:11
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

anda_skoa
29th September 2014, 12:33
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,
_

dhillon_5230
29th September 2014, 15:54
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

anda_skoa
29th September 2014, 16:42
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,
_

dhillon_5230
30th September 2014, 08:12
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.