Friday, March 26, 2010

Unit Testing quirks: Why doesn't the makefile know how to build the ".moc" file.

I'm following the sample at the Qt site on how to make a unit test. But mine continually failed. Of course I'm less than comfortable with the two magic lines at the end of the source file:

Finally, to make our test case a stand-alone executable, the following two lines are needed:

 QTEST_MAIN(TestQString)
#include "testqstring.moc"

The QTEST_MAIN() macro expands to a simple main() method that runs all the test functions. Note that if both the declaration and the implementation of our test class are in a .cpp file, we also need to include the generated moc file to make Qt's introspection work.

Note the part at the end "if both the declaration and implementation ... are in a .cpp file." what's missing is that "if the declaration and implementation are in separate .h and .cpp files, then that final include is unnecessary."

That's the danger of having magic in the build system.

I've uploaded a sample unit test to my SkyDrive. So far I've not found any buildable projects on the web.

A little bitty Java has some good notes on Qt Unit Testing.

KDE TechBase has another intro here.

Here's a description of why the include's not necessary when there's both a header and cpp file in the test.

No comments:

Post a Comment