13.4.1.  Trolltech Model Testing Tool

[ fromfile: treemodels.xml id: modeltest ]

The methods you implement in a model are called by views you may not have tested, or use-cases that you have not tried. Because the data values are often driven by the user, it can be helpful to test your model with the "modeltest" tool. It finds common implementation errors quickly and gives you an indication of how to fix them.

Figure 13.13.  ModelTest in the Debugger

ModelTest in the Debugger

Figure 13.13 shows what happens when you run the ObjectBrowser example from Example 13.21 in the debugger from QtCreator, with the ModelTest tool. The failed assertions and aborts might be deeper in the stack trace, so you need a debugger to see the full stack trace. Typically, there will be code comments right before the aborted line that indicate what test it was performing at that point. The included readme.txt file, shown in Example 13.23 contains brief instructions for using ModelTest.

Example 13.23. src/libs/modeltest/readme.txt

To Use the model test do the following:

1) Include the pri file at the end of your project pro file using the
include() command like so:

include(../path/to/dir/modeltest.pri)

2) Then in your source include "modeltest.h" and instantiate ModelTest
with your model so the test can live for the lifetime of your model.
For example:

#include <modeltest.h>

QDirModel *model = new QDirModel(this);
new ModelTest(model, this);

3) That is it.  When the test finds a problem it will assert.
modeltest.cpp contains some hints on how to fix problems that the test
finds.