Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.
Unit Testing: Difference between revisions
No edit summary |
No edit summary |
||
Line 3: | Line 3: | ||
= Unit testing with QTest = | = Unit testing with QTest = | ||
For a basic overview how to write unit tests with qtestlib, you should refer to the | For a basic overview how to write unit tests with qtestlib, you should refer to the "official manual.":http://doc.qt.io/qt-5.0/qttestlib/qtest-tutorial.html | ||
== Test Output == | == Test Output == | ||
Line 9: | Line 9: | ||
'''Output to a txt file''' | '''Output to a txt file''' | ||
<code>QStringList testCmd; | <code>QStringList testCmd; | ||
QDir testLogDir; | |||
testLogDir.mkdir("UnitTest_Results"); | |||
testCmd<<" "<<"-o" <<"UnitTest_Results/test_log.txt"; | |||
QTest::qExec(myTestClass,testCmd);</code> | |||
'''Output in xml format:''' | '''Output in xml format:''' | ||
<code>QStringList testCmd; | <code>QStringList testCmd; | ||
QDir testLogDir; | |||
testLogDir.mkdir("UnitTest_Results"); | |||
testCmd<<" "<<"-xml" <<"-o" <<"UnitTest_Results/test_log.xml"; | |||
QTest::qExec(myTestClass,testCmd);</code> | |||
The above code will run the test and the output will be generated in xml format. The xml file can be parsed and the details can be displayed in an appropriate way as per the requirements. | The above code will run the test and the output will be generated in xml format. The xml file can be parsed and the details can be displayed in an appropriate way as per the requirements. | ||
Line 23: | Line 31: | ||
The snippet below goes to CMakeLists.txt, and specifies files that are used for the test. | The snippet below goes to CMakeLists.txt, and specifies files that are used for the test. | ||
<code> | <code> | ||
enable_testing(true) | |||
include_directories( test ) | |||
set(test_additional | set(test_additional | ||
some_dependency.cpp | |||
some_other_dependency.cpp | |||
) | |||
add_test(dummytest test/DummyTest.cpp) | |||
</code> | |||
If you do not want to have tests built all the time, remove <code>enable_testing()<code> and run cmake with <code>cmake .. -DENABLE_TESTING=true<code> in order to build tests. | If you do not want to have tests built all the time, remove <code>enable_testing()<code> and run cmake with <code>cmake .. -DENABLE_TESTING=true<code> in order to build tests. | ||
Line 31: | Line 46: | ||
The macro below uses variable ${test_additional} to give additional source files for test to compile, the files that are needed for test. It adds the test with </code>add_test()</code> to ctest test “database”. | The macro below uses variable ${test_additional} to give additional source files for test to compile, the files that are needed for test. It adds the test with </code>add_test()</code> to ctest test “database”. | ||
<code> | <code> | ||
MACRO (add_test testname testsrc) | |||
SET (test_${testname}_SRCS ${testsrc}) | |||
qt4_automoc(${test_${testname}_SRCS}) | qt4_automoc(${test_${testname}_SRCS}) | ||
add_executable(test_${testname} ${test_${testname}_SRCS} ${test_additional}) | |||
target_link_libraries(test_${testname} ${QT_QTCORE_LIBRARY} | |||
${QT_QTTEST_LIBRARY} ${QT_QTGUI_LIBRARY} | |||
${GSOAP_LIBRARIES} ${QT_QTLOCATION_LIBRARY}) | |||
ADD_TEST(test_${testname} test_${testname}) | ADD_TEST(test_${testname} test_${testname}) | ||
ENDMACRO (add_test) | |||
</code> | |||
Tests can be compiled with <code>make<code> and run either directly by name of the test (test_${testname} as specified in the macro) or every test as batch with <code>make test<code> | Tests can be compiled with <code>make<code> and run either directly by name of the test (test_${testname} as specified in the macro) or every test as batch with <code>make test<code> | ||
[[Category:Howto]] | [[Category:Howto]] |
Revision as of 09:44, 25 February 2015
Unit testing with QTest
For a basic overview how to write unit tests with qtestlib, you should refer to the "official manual.":http://doc.qt.io/qt-5.0/qttestlib/qtest-tutorial.html
Test Output
Output to a txt file
QStringList testCmd;
QDir testLogDir;
testLogDir.mkdir("UnitTest_Results");
testCmd<<" "<<"-o" <<"UnitTest_Results/test_log.txt";
QTest::qExec(myTestClass,testCmd);
Output in xml format:
QStringList testCmd;
QDir testLogDir;
testLogDir.mkdir("UnitTest_Results");
testCmd<<" "<<"-xml" <<"-o" <<"UnitTest_Results/test_log.xml";
QTest::qExec(myTestClass,testCmd);
The above code will run the test and the output will be generated in xml format. The xml file can be parsed and the details can be displayed in an appropriate way as per the requirements.
Running and compiling QTests with CTest
CTest, by design, is only a facility for testing. For that it scales to very different unit tests frameworks, and works out of box with QTest.
The snippet below goes to CMakeLists.txt, and specifies files that are used for the test.
enable_testing(true)
include_directories( test )
set(test_additional
some_dependency.cpp
some_other_dependency.cpp
)
add_test(dummytest test/DummyTest.cpp)
If you do not want to have tests built all the time, remove
enable_testing()<code> and run cmake with <code>cmake .. -DENABLE_TESTING=true<code> in order to build tests.
The macro below uses variable ${test_additional} to give additional source files for test to compile, the files that are needed for test. It adds the test with
add_test() to ctest test “database”.
MACRO (add_test testname testsrc)
SET (test_${testname}_SRCS ${testsrc})
qt4_automoc(${test_${testname}_SRCS})
add_executable(test_${testname} ${test_${testname}_SRCS} ${test_additional})
target_link_libraries(test_${testname} ${QT_QTCORE_LIBRARY}
${QT_QTTEST_LIBRARY} ${QT_QTGUI_LIBRARY}
${GSOAP_LIBRARIES} ${QT_QTLOCATION_LIBRARY})
ADD_TEST(test_${testname} test_${testname})
ENDMACRO (add_test)
Tests can be compiled with make
and run either directly by name of the test (test_${testname} as specified in the macro) or every test as batch with
make test