Assignment Chef icon Assignment Chef
All English tutorials

Programming lesson

Unit Testing in C++ with GoogleTest: White Box and Black Box Testing for COP 4600

Learn unit testing in C++ with GoogleTest, covering white box and black box testing techniques. This tutorial walks through installation, writing test cases, and testing an airplane booking system.

unit testing C++ GoogleTest tutorial white box testing black box testing COP 4600 C++ unit testing GoogleTest installation test assertions airplane booking system testing divisibility function test C++ testing framework software testing test-driven development C++ programming developer testing code quality

Introduction to Unit Testing in C++

When writing code, particularly for complex systems, it is imperative to continuously check that your work is correct. Unit tests allow developers to write tests that demonstrate that output matches specifications and mitigate the risk that changes in one area of the codebase cause unintended consequences. This tutorial introduces unit testing in C++ using the GoogleTest framework, focusing on white box and black box testing as covered in COP 4600 Ex4.

Part 1: Installing GoogleTest

Before writing tests, you need to install GoogleTest. Run the following commands:

sudo apt-get install cmake
git clone https://github.com/google/googletest.git -b v1.13.0
cd googletest
mkdir build
cd build
cmake .. -DBUILD_GMOCK=OFF
make
sudo make install

This installs GoogleTest in /usr/local. You can then compile your test files by linking against the GoogleTest library.

Anatomy of a Unit Test

A unit test is a function that verifies the output of code (the subject under test) using assertions. If an assertion fails, the test fails. Tests are compiled into a binary and executed. Example for a factorial function:

#include "gtest/gtest.h"
#include "Factorial.h"
TEST(FactorialTest, HandlesZeroInput) {
  int result = Factorial(0);
  ASSERT_EQ(result, 1);
}

Tests are grouped into suites (here FactorialTest) and have a test name (here HandlesZeroInput). GoogleTest provides a main function, so you only need to write TEST macros.

White Box Testing: Divisibility Function

White box testing uses knowledge of the implementation to write tests. Consider the function isDivisible(int a, int b) that returns b % a == 0. You need to test boundary conditions. Write tests for:

  1. A positive, B positive, A divides B (expect true)
  2. A positive, B positive, A does not divide B (expect false)
  3. A positive, B zero (expect true, since 0 % a == 0)
  4. A negative, B positive, A divides B (expect true)

Example test:

TEST(DivisibilityTest, PositiveDividesPositive) {
  EXPECT_TRUE(isDivisible(2, 4));
}

Think of this like testing a game leaderboard: you need to check edge cases like zero points or negative scores.

Black Box Testing: Airplane Booking System

Black box testing treats the system as a black box. You write tests based on specifications without knowing the internal code. For an airplane booking system, you need to test:

  • Query returns all available bookings from source to destination
  • Query excludes bookings with full flights
  • Purchase decrements capacities
  • Connections have a layover of at least 2 hours
  • Connections are only returned when no direct flights exist

Example test for query:

TEST(BookingTest, QueryReturnsDirectFlights) {
  Airport jfk("JFK"), lax("LAX");
  Flight f(jfk, lax, 10, 5);
  BackEnd backend;
  backend.addFlight(f);
  auto results = backend.query(jfk, lax);
  EXPECT_EQ(results.size(), 1);
}

This is like testing a flight search app—you want to ensure it shows the correct options.

Conclusion

Unit testing is essential for reliable software. With GoogleTest, you can write white box tests for internal logic and black box tests for system behavior. Practice by writing tests for edge cases, and remember to test both success and failure scenarios. Happy testing!