User Tools

Site Tools


This is an old revision of the document!

Draft: this page is under construction…

Implementing Unit Tests

For Mail2Voice Next, we introduced unit tests. Units tests are useful to ensure that every classes/methods/functions behave the way we think they are supposed to. It helps the developers to design their code when writing the tests before coding the actual implementation. It helps the developers verifying that the code still works after modifications. Ultimately, it helps developers to detect regressions in the code. Unit tests are one the first tools to use in Quality Assurance.

The unit tests written for Mail2Voice are base on QTest suite.

HOWTO : writing a unit test for a class

Each class of Mail2Voice must have its dedicated unit tests to check every method in relevant scenarios (to test edge cases, side effects, etc.).


Let's say we have a class named MyClass with the following header :

class MyClass
    MyClass(); // Constructor initializing members
    void increment(); // Add 1 to m_count
    void setText(const QString& text); // Set m_text to text
    int count(); // Returns current m_count value
    QString text(); // Returns current m_text value
    int m_count;
    QString m_text;

MyClass is a very simple class that does two things : incrementing an internal variable and setting an internal text. Nothing special there, isn't it? Why should we care about testing such a trivial code?

Wait, mistakes are common and even if you are sure of your code, are you sure that no one else won't introduce errors in your code? That, is where unit tests are useful : ensuring your code is correct and will stay correct over time. Saving you a lot of time of debugging in the future.

What to test?

So, what do we have to test exactly? Basically every methods:

  • first, the constructor because it is in charge of initializing an object of type MyClass,
  • the count() and text() methods to make sure they return correct values,
  • the increment() and setText(const QString& text) to verify they alter members correctly.

To write a unit test, you have to ask yourself what behavior is expected for each method. Let's do this exercise:

  • MyClass(): the constructor must initialize the object, so it must initialize the members m_count and m_text. But, to which values? This have to be specified somewhere otherwise the objects could be inconsistent.
    • For our example, we will pretend that m_count must be initialize to 0 and m_text to “default text”;
  • increment(): this must add just 1 to m_count. But m_count is an int, meaning it can be negative. What if we increment m_count until 2147483647 (assuming int is on 32 bits) and the increment again? Does the increment method will just put m_count to the minimum negative value? Or will it refuse to increment again, leaving m_count to 2147483647?
    • For our example, we choose the second option: never go to negative value.
  • setText(const QString& text): now you get it, we will assume this method accept any strings except empty ones.
  • count() and text() methods must always return the current value.

Coding the unit tests

In the Mail2Voice Qt project, we have a subproject named unittests. For each class, there is a dedicated test class. The header of the test class will be like this:

#include "myClass.h"
#include <QObject>
class TestMyClass : public QObject
    Q_OBJECT // Because we derive from QObject and want Qt features
        explicit TestMyClass(QObject *parent = 0);
    // Below are the unit tests. Each slot will be called via the QTest suite.
    private Q_SLOTS:
        void default_constructor();
        void increment();
        void incrementMax();
        void setText();
        void setTextEmpty();
unit_tests.1473335969.txt.gz · Last modified: 2020/10/11 09:50 (external edit)