Exercise: Unit Testing with Google Test

Outline

Introduction

In your previous software engineering courses, you should have at least seen and hopefully even used unit testing. You should also understand the important role that testing (and unit testing in particular) plays in agile software development. In the context of this course, you will be expected to create tests built upon the Google Test framework for C++. In this exercise, you will see how to integrate unit testing using the Google Test framework into a C++ project built using CMake. You will gain first hand experience writing some simple unit tests, and you will be provided with resources that allow you to use more complex testing patterns within your unit tests.

For outside information, refer to:

If you are new to CMake, you may wish to also look at this CMake exercise. Please bear in mind that you should be using out of source builds. You will ultimately submit only the source code of your project and not any of the build artifacts.

Submissions that do not compile and run from a clean build directory using the commands

cmake <path/to/submission/>
make
bin/runAllTests

will receive 0 points.

Exercise Structure

The provided files for this exercise illustrate a reasonable way to incorporate Google Test into a project. The project itself implements a small library with a variety of different functionalities. The source files for this library reside in the lib/ directory. Header files for the classes in the library can be found in the include/ directory. Your tasks as a part of this exercise will be to test this project using the facilities in the Google Test framework. All of the tests for the project will live in the test/ directory. The tests directory also includes the source code of Google Test and Google Mock inside test/lib/. Including the source code of Google Test in the project and compiling it as a part of the project has two key advantages. (1) It ensures that the version of Google Test used to run the test suite is consistent. (2) It avoids some subtle corner cases involved with compiling, linking, and testing native code specifically.

All of the files that you need to modify can be found in the test/ directory. The files inside lib/ will be replaced during the grading process. Remember to follow the instructions carefully, as projects will be graded (mostly) automatically. Specifically, make sure to spell fixture or test group names exactly as specified.

Tasks

To create a set of related test cases, you should create a C++ source file in the test/ directory. Make sure to include gtest/gtest.h and/or gmock/gmock.h as necessary. All of the source files for your tests should then be added to the list of source files for creating the runAllTests program in test/CMakeLists.txt. You can do this by editing the function call to add_executable in that file. Notice that there is also an add_test function call in test/CMakeLists.txt. You do not need to modify this. The libraries for Google Test and Google Mock will be linked in by the CMakeLists.txt configuration already, as you can see on the target_link_libraries lines.

Task 1

Open the files include/Triangle.h and lib/Triangle.cpp. These files provide the declaration and definition of a simple Triangle class. The constructor of Triangle takes in the integral lengths of the sides of a Triangle, requiring that the first side be the longest and that all sides satisfy the triangle inequality. Note that there are bugs in the getPerimeter(), getArea(), and getKind() methods. You must complete the following tasks for the Triangle class using the Google Test framework:

Each of these test cases should be in its own test function, and the group name or test fixture for the tests should be called TriangleTests. This name will be used to automatically extract individual tests during grading.

Task 2

Now consider the function satisfiesHailstone() declared in Hailstone.h and defined in Hailstone.cpp. Create a set of related tests such that every statement in satisfiesHailstone() is executed by at least one test. The group name or test fixture for the tests should be called HailstoneTests. Make sure the name is correct.

Task 3

Finally, consider the performAwardCeremony function declared in Awards.h and defined in Awards.cpp. This function reads a list of names from a sequence and awards medals to the first three names. Write a test case that makes sure the ceremony runs as intended. You will need to create a stub for RankList and a mock for AwardCeremonyActions. The methods of AwardCeremonyActions should be called exactly once each in the order: playAnthem(), awardBronze(), awardSilver(), awardGold(), and turnOffTheLightsAndGoHome(). The getNext() method of RankList should be called three times, and the names returned should be passed to awardBronze(), awardSilver(), and awardGold() in the same order they are read from the list. I highly recommend that you consult the Google Mock Dummies Guide in order to make sure that you (1) correctly create the test fakes, (2) validate that the methods were called, and (3) validate that they were called in the right order and with the right arguments. The group name or test fixture for the test should be called AwardsTests.

Submission

To submit your exercise, create an archive of the directory containing your source (not your build), and submit it via CourSys. This should contain all of the provided project files along with your additions and modifications necessary to run your tests.

NOTE: Your archive should contain the googletest-exercise/ directory from the project archive as well as its subdirectories. This is necessary for your submission to be graded. Again, this should not include your build artifects.