Showing posts with label QTest. Show all posts
Showing posts with label QTest. Show all posts

Creating Unit Test using Qt Test

Introduction

Hello readers, we discussed some basic introduction and concepts about Qt Test in my previous article. If you are new to the Qt Test, I recommend you to click the below link to go through my previous article before continuing this.

In this article, we are going to write an example application with unit test capabilities by using Qt Test Framework.

Creating a Qt Test Application

Creating an Qt application with testing capability is very simple as discussed in the previous article. Qt creator provides project template for creating the application that contains test classes in-turn test the classes of production code. For better understanding, we can discuss with a simple example below:

Create sample MathCalc class, which is a production code given below:

#ifndef MATHCALC_H
#define MATHCALC_H

class MathCalc
{
public:
    MathCalc(int a = 0, int b = 0) : m_nA(a), m_nB(b) { }


    void setData(int a, int b) {
        m_nA = a;
        m_nB = b;
    }

    int Sum() const { return m_nA + m_nB; }
    int Diff() const { return m_nA - m_nB; }
    int Mult() const { return m_nA * m_nB; }
    int Div() const { return m_nA / m_nB; }

private:
    int m_nA;
    int m_nB;
};

#endif // MATHCALC_H

The above MathCalc is the production code needs to be tested. For testing the MathCalc class, we are going to create the new test class called TestMathCalc class.

#include <QtTest>

#include "mathcalc.h"

class TestMathCalc : public QObject
{
    Q_OBJECT

public:
    TestMathCalc();
    ~TestMathCalc();

private slots:
    void test_sum();
    void test_mult();

private:
    MathCalc m_objMathCalc;
};

TestMathCalc::TestMathCalc()
{

}

TestMathCalc::~TestMathCalc()
{

}

void TestMathCalc::test_sum()
{
    // sum default
    QCOMPARE(m_objMathCalc.Sum(), 0 + 0);

    // sum after setting A and B
    const int A = 10;
    const int B = 20;
    m_objMathCalc.setData(A, B);

    QVERIFY2(m_objMathCalc.getA() == A, "Operand A doesn't match");
    QVERIFY2(m_objMathCalc.getB() == B, "Operand B doesn't match");

    QCOMPARE(m_objMathCalc.Sum(), A + B);
}

void TestMathCalc::test_mult()
{
    const int A = 5;
    const int B = 6;
    m_objMathCalc.setData(A, B);

    QCOMPARE(m_objMathCalc.Sum(), A + B);
}

QTEST_APPLESS_MAIN(TestMathCalc)

#include "tst_testmath.moc"

Run the above code & check the output and it will look like below:


Now Change the line number 41 like below to simulate the failure condition. 

    QVERIFY2(m_objMathCalc.getA() == 0, "Operand A doesn't match");

You will get the output like below:


Conclusion

We experimented with a small example unit test project by using Qt Test Framework. This will give an idea and more confidence to you for exploring more, by using Qt Test. 

You can download the complete source code from the GitHub.

Introduction to Qt Test

What is Unit Testing ?

Unit Testing is a process in which a way to test the small pieces of code that can be logically isolated from the system. Piece of code (known as unit) may be an individual function, method, procedure, module, or object. The purpose of task is to validate each unit of the software code performs as expected. Unit Testing is done during the development phase of an application by the developers.

What is Qt Test ?

Qt Test is a unit testing framework released from the Qt developers to test the applications or libraries based on Qt framework. It provides all the functionality commonly found in unit testing frameworks as well as extensions for testing graphical user interfaces.

Create Qt Test Project

Qt Creator facilitate the wizard to create the project for unit testing. You can use the "Auto Test Project" wizard to create the Qt Test project.

  • Select File > New Project > Other Project > Auto Test Project > Choose to create a project with boilerplate code for a Qt test.

                                     

  • In the Project and Test Information dialog, specify settings for the project and test:
    • In the Test framework field, select Qt Test.
    • For a Qt test, select the GUI Application check box to create a Qt application.
    • In 'Test case name' field, enter the name of the test case.
    • Select the 'Requires QApplication' check box to add the include statement for QApplication to the main.cpp file of the project.
    • Select the 'Generate initialization and cleanup code' checkbox to add functions to your test that are executed by the testing framework to initialize and clean up the test.
    • Select the build system to use for building the project: qmake, CMake, or Qbs.


Qt Creator creates the test project in the specified project directory. Edit the .cpp file to add private slots for each test function in your test.

Creating a Test
Creating a test, subclass QObject and add one or more private slots in your test class. Each private slot is a test function in your test. In addition, you can define the following private slots that are not treated as test functions. When present, they will be executed by the testing framework and can be used to initialize and clean up either the entire test or the current test function.

  • initTestCase() will be called before the first test function is executed.
  • initTestCase_data() will be called to create a global test data table.
  • cleanupTestCase() will be called after the last test function was executed.
  • init() will be called before each test function is executed.
  • cleanup() will be called after every test function.

Every test should leave the system in a usable state, so it can be run repeatedly. Cleanup operations should be handled in cleanupTestCase(), so they get run even if the test fails.

QTest Namespace
Qt Test Framework provides QTest namespace and QAbstractItemModelTester, QSignalSpy classes to facilities various functions and declarations for unit testing. 

  • QTest - All public methods are in the QTest namespace. 
  • QSignalSpy - Enables introspection for Qt's signals and slots.
  • QAbstractItemModelTester - Allows for non-destructive testing of item models.
Finally....
I am so exhausted with lots of theory. So lets break it here and we meet in the next session with some interesting examples for better understanding. Bye...

Catagory

More »

Catagory