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)