Cmake Study Notes (6)

Source: Internet
Author: User
Tags qtest

I hope this is the last obstacle that hinders reading the source code of shiboken and pyside.

Learn the unit test part of cmake.

Easy to use

The simplest way to use ctest is to add the command in cmakelists.txt:

enable_testing()
  • This command must be in the source code root directory.

From this moment on, you can add the add_test command to the project.

add_test(NAME <name> [CONFIGURATIONS [Debug|Release|...]]           [WORKING_DIRECTORY dir]           COMMAND <command> [arg1 [arg2 ...]])
  • Name specifies a name
  • Debug | release controls the configuration to take effect.
  • Dir sets the working directory
  • Command
    • If it is an executable program target, cmake will replace it with the full path of the generated program
    • The following parameters can use the $ <...> syntax. For example, $ <target_file: TGT> indicates the full name of the TGT Target.

Apiextractor

Take apiextractor as an example to learn how to use ctest.

Content snippets of the top-layer cmakelists.txt file:

option(BUILD_TESTS "Build tests." TRUE)if (BUILD_TESTS)    enable_testing()    add_subdirectory(tests)endif()

Create option to allow users to control whether to enable unit test. If it is enabled, add it to the tests subdirectory. We can see its cmakelists.txt file.

  • First, create a declare_test macro.

    • Use qt4_automoc for MOC Processing
    • Generate executable files
    • Call add_test to join the test
macro(declare_test testname)    qt4_automoc("${testname}.cpp")    add_executable(${testname} "${testname}.cpp")    include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} ${apiextractor_SOURCE_DIR})    target_link_libraries(${testname} ${QT_QTTEST_LIBRARY} ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} apiextractor)    add_test(${testname} ${testname})endmacro(declare_test testname)
  • It will be simple in the future. The required configuration file uses the copyonly of configure_file.
declare_test(testabstractmetaclass)declare_test(testabstractmetatype)declare_test(testaddfunction)declare_test(testarrayargument)declare_test(testcodeinjection)configure_file("${CMAKE_CURRENT_SOURCE_DIR}/utf8code.txt"                "${CMAKE_CURRENT_BINARY_DIR}/utf8code.txt" COPYONLY)declare_test(testcontainer)
Qt unit test

The qtestlib module is very easy to use. Here we are a little bit different from qmake.

  • When using qmake, we only need one source file. For example, when testing the qstring class, write a testqstring. cpp file.
 #include <QtTest/QtTest> 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"

Then, enable the testlib module in the Pro file. The others are the same as the common QT program.

  • When cmake is used, we divide it into two files
//testqstring.h #include <QtTest/QtTest> class TestQString: public QObject {     Q_OBJECT private slots:     void toUpper(); };

And

//testqstring.cpp void TestQString::toUpper() {     QString str = "Hello";     QCOMPARE(str.toUpper(), QString("HELLO")); } QTEST_MAIN(TestQString) #include "testqstring.moc"

Then the processing method is the macro we saw earlier.

Qtest macro

Take a look at the qtest macro.

  • Qtest_appless_main
  • Qtest_noop_main
  • Qtest_main
#define QTEST_APPLESS_MAIN(TestObject) /int main(int argc, char *argv[]) /{ /    TestObject tc; /    return QTest::qExec(&tc, argc, argv); /}#define QTEST_NOOP_MAIN /int main(int argc, char *argv[]) /{ /    QObject tc; /    return QTest::qExec(&tc, argc, argv); /}#define QTEST_MAIN(TestObject) /int main(int argc, char *argv[]) /{ /    QCoreApplication app(argc, argv); /    TestObject tc; /    return QTest::qExec(&tc, argc, argv); /}

In the end, qtest: qexec is called, and manual has introduced many of them (omitted ).

Reference
  • Http://www.itk.org/Wiki/CMake_Testing_With_CTest

  • Http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:add_test

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.