Unit Testing: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
m (Fix formatting)
 
(7 intermediate revisions by 5 users not shown)
Line 1: Line 1:
'''English''' [[Unit_Testing_Spanish|Spanish]] [[Unit_Testing_Bulgarian|Български]]
'''English''' [[Unit_Testing_Spanish|Spanish]] [[Unit_Testing_Bulgarian|Български]]


= Unit testing with QTest =
= 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
For a basic overview how to write unit tests with qtestlib, you should refer to the [http://doc.qt.io/qt-5/qtest-tutorial.html official manual.]


== Test Output ==
== Test Output ==
Line 9: Line 10:
'''Output to a txt file'''
'''Output to a txt file'''


<code>QStringList testCmd;<br />QDir testLogDir;<br />testLogDir.mkdir(&quot;UnitTest_Results&amp;quot;);<br />testCmd&amp;lt;&lt;&quot; &quot;&lt;&lt;&quot;-o&amp;quot; &lt;&lt;&quot;UnitTest_Results/test_log.txt&amp;quot;;<br />QTest::qExec&amp;amp;#40;myTestClass,testCmd&amp;amp;#41;;</code>
<pre>QStringList testCmd;
QDir testLogDir;
testLogDir.mkdir("UnitTest_Results");
testCmd<<" "<<"-o" <<"UnitTest_Results/test_log.txt";
QTest::qExec(myTestClass,testCmd);</pre>


'''Output in xml format:'''
'''Output in xml format:'''


<code>QStringList testCmd;<br />QDir testLogDir;<br />testLogDir.mkdir(&quot;UnitTest_Results&amp;quot;);<br />testCmd&amp;lt;&lt;&quot; &quot;&lt;&lt;&quot;-xml&amp;quot; &lt;&lt;&quot;-o&amp;quot; &lt;&lt;&quot;UnitTest_Results/test_log.xml&amp;quot;;<br />QTest::qExec&amp;amp;#40;myTestClass,testCmd&amp;amp;#41;;</code>
<pre>QStringList testCmd;
QDir testLogDir;
testLogDir.mkdir("UnitTest_Results");
testCmd<<" "<<"-xml" <<"-o" <<"UnitTest_Results/test_log.xml";
QTest::qExec(myTestClass,testCmd);</pre>


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.


== Running and compiling QTests with CTest ==
== Qt 4: 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.
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.
Line 23: Line 32:
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><br />enable_testing(true)<br />include_directories( test )
<pre>
enable_testing(true)
include_directories( test )
 
set(test_additional
some_dependency.cpp
some_other_dependency.cpp
)
add_test(dummytest test/DummyTest.cpp)
</pre>
 
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.


set(test_additional<br /> some_dependency.cpp<br /> some_other_dependency.cpp<br />)<br />add_test(dummytest test/DummyTest.cpp)<br /></code>
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".


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.
<pre>
MACRO (add_test testname testsrc)
SET (test_${testname}_SRCS ${testsrc})


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”.
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})


<code><br />MACRO (add_test testname testsrc)<br /> SET (test_$&amp;#123;testname&amp;#125;_SRCS $&amp;#123;testsrc&amp;#125;)
ADD_TEST(test_${testname} test_${testname})
ENDMACRO (add_test)
</pre>


qt4_automoc(${test_${testname}_SRCS})<br /> add_executable(test_${testname} ${test_${testname}_SRCS} ${test_additional})<br /> target_link_libraries(test_${testname} ${QT_QTCORE_LIBRARY}<br /> ${QT_QTTEST_LIBRARY} ${QT_QTGUI_LIBRARY}<br /> ${GSOAP_LIBRARIES} ${QT_QTLOCATION_LIBRARY})
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>


ADD_TEST(test_${testname} test_${testname})<br />ENDMACRO (add_test)<br /></code>
== Qt 5: Building Qt Tests ==


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>
See [https://doc.qt.io/qt-5/qtest-overview.html#building-a-test Building a Test] in the [https://doc.qt.io/qt-5/qttest-index.html Qt Test documentation].


[[Category:Howto]]
[[Category:HowTo]]

Latest revision as of 18:33, 21 December 2020

English Spanish Български

Unit testing with QTest

For a basic overview how to write unit tests with qtestlib, you should refer to the official manual.

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.

Qt 4: 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()

and run cmake with

cmake .. -DENABLE_TESTING=true

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

Qt 5: Building Qt Tests

See Building a Test in the Qt Test documentation.