Pages

Unit Testing Your QT Application And Analyzing the Coverage

Monday, May 2, 2016

Testing your GUI application is undoubtedly a crucial, but difficult part of front end development life cycle. Unlike back-end development where you can mock or simulate the inputs and outputs, front end development requires a different method for simulating the input and validating the output.
 When testing a gui based application one can choose to

1.Simply test the model part of the application, which probably is shared by other back-end applications as well.
2. Isolate test the control to model part of the application (In a MVC architecture application)
3. Simulate user inputs and test the application from input to output

If we think about it option 3 is more towards a component test where you test your application end to end. If the developers practice testing with the code writing, chances are that unit tests will already be written to test the model classes before the GUI is even developed. It should be possible to use any existing testing framework to test the model of the application.
Testing control to model testing will require View part of your application mocked, which will require some additional effort during designing.

QT Test framework supports testing your GUI part of the application. Following is a method to setup a test project for your already developed application using QT test framework.
1. Create a separate QT project for the testing.
2. Add the test lib to .pro file
QT       += core gui testlib
3. Include your application to the test project pro file
include(/home/kanishka/Projects/CRM/CRM.pro)
4. Include the application sources, so that it could compile
INCLUDEPATH += "/home/kanishka/Projects/MyApp"
5. Comment the main file from the applications pro file during testing (I couldn't find any other way to continue the process)
6. Add Coverage flags to pro file
QMAKE_CXXFLAGS += -g -Wall -fprofile-arcs -ftest-coverage -O0

QMAKE_LFLAGS += -g -Wall -fprofile-arcs -ftest-coverage  -O0

LIBS += \
    -lgcov

After these configurations are complete, you can add classes to the test project. A test class is a normal class deriving from the QObject. Users are supposed to write the tests as private 
slots.
class NewTest : public QObject
{
    Q_OBJECT
public:
    explicit NewTest(QObject *parent = 0);

private slots:
    void initTestCase(); //Running once for the whole test case
    void init(){} // Runs for each test
    void DoAddNewTest(); //Test
    void cleanup(){}// Runs for each test
    void cleanupTestCase();//Running once for the whole test case

};

To run each test
NewTest myNewTestObject;
QTest::qExec(&myNewTestObject,argc, argv);


Writing a test and verifying is very well documented. Please refer the verification macros in http://doc.qt.io/qt-5/qtest.html

Coverage with LCOV
We have already configured g++ to provide gcov information during compilation. During compilation these information will be compiled to *.gcno files in your build directory.  When you run the tests coverage information will be written to *.gcda files. To convert them to LCOV files you need to run lcov utility.
lcov --base-directory . --directory . -c -o myapplication.info
genhtml -o ../my_html_output_dir  -t "My Application Test Coverage" --num-spaces 4 myapplication.info
This will dump your line coverage information using  LCOV  to ../my_html_output_dir which you can rename.


0 comments:

Post a Comment

 
Copyright © පොඩ්ඩාගේ ලෝකය..........
Blogger Theme by BloggerThemes Sponsored by Busy Buzz Blogging