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
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