Home > Articles, Libraries & Frameworks > Black Box Unit Testing with the QT Framework.

Black Box Unit Testing with the QT Framework.

Black Box testing and general testing is a key step in the iterative development methodologies (e.g. UP, XP, Agile) as it performs both the validation and verification of units in a system.

Below, we quote a simple example (to familiarize yourself with the process) of black box testing in one software unit called ‘Number’. For this implementation we used the QT Framework and the QTest library.

Below is the interface of the software unit ‘Number’:

class Number {
  public:
    Number ();
    Number (int x);
    Number (Number & obj);
    ~Number ();

    void setValue (int x);
    int getValue ();

    void add (Number & obj);

  private:
    int x;
};

Follows the implementation of the software unit ‘Number’:

#include "Number.h"

Number::Number () { x = 0; }

Number::Number (int x) { this->x = x; }

Number::Number (Number & obj) { x = obj.x; }

Number::~Number () { x = 0; }

void Number::setValue (int x) { this->x = x; }

int Number::getValue () { return x; }

void Number::add (Number & obj) { x += obj.x; }

Below is the interface of the control unit ‘TestNumber’:

#include <QtTest>

#include "Number.h"

class TestNumber : public QObject {
  Q_OBJECT

  private slots:
    void checkAdd ();
    void checkSet ();
    void checkGet ();
};

Follows the implementation of the control unit ‘TestNumber’:

#include "TestNumber.h"

void
TestNumber::checkAdd () {
  int a=3, b=2, c=a+b;
  Number A (a), B (b);
  B.add (A);

  QVERIFY (B.getValue () == c);
}

void
TestNumber::checkSet () {
  int a = 2;
  Number A;
  A.setValue (a);

  QVERIFY (A.getValue () == a);
}

void
TestNumber::checkGet () {
  int a = 2;
  Number A (a);
  QVERIFY (A.getValue () == a);
}

QTEST_MAIN (TestNumber)
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: