The following exercise shows multiple ways of building tests for a Session class. The Session class performs activities over a period of time and then issues a report that includes the time period of the session. The session details are not shown here, only the reported session time.

Each approach is on a different branch:

To see what branch you are on, use the git command:

To switch to another branch, e.g., dip, use the git command:

To see what files are on that branch, use the bash command:

Spend a minute switching among the various branches. Use the git branch to verify what branch you are on. If you look at the files on each branch, you will see common filenames and files that only exist on an individual branch. All of the necessary files already exist on each branch, so no files are added during the exercise.

Branch main: Session Testing without DIP

  1. Build the code and run the 2-second session test. You can use the time command: Record the timing of three runs of the test program, and note the median value. From the time command, use the real time. Compare the timings to others on your team.
  2. The test for the 5-second session in Session5SecondTest.cpp is incomplete. Complete it following the example in Session2SecondTest.cpp. Build the code, get the test case to pass, and run the 5-second session test: Record the timing of three runs of the test program, and note the median value. Compare the timings to others on your team. Commit your code changes with the commit command:
  3. Let's say we would have a 1-hour session test. Record how long you think it would take to run the test. It should be obvious why we will not have a 1-hour session test using this approach.

Branch dip: Session Testing with DIP

  1. Change to the branch dip. Make sure to verify that you are on the correct branch.
  2. View the files Session.hpp, Clock.hpp, Session2Second.cpp, and TwoSecondClock.cpp, and note that instead of the class Session directly using the std::chrono real-time clock, all clock duties have moved to the class Clock. This implies:
    • The class Session now has a dependency on the class Clock
    • This class Clock dependency must be set up when the Session is created
    • The actual clock used is the TwoSecondClock. Build and run the existing test Session2SecondTest. Record the timing of three runs of the test program, and note the median value. Compare the timings to others on your team.
  3. The test for the 5-second session in Session5SecondTest.cpp is incomplete. Complete it following the example in Session2SecondTest.cpp. Build the code, get the test case to pass, and run the 5-second session test: Record the timing of three runs of the test program, and note the median value. Compare the timings to others on your team. Commit your code changes with the commit command:
  4. Now, we can write a practical 1-hour session test. The test for the 1-hour session in Session1HourTest.cpp is incomplete. Complete it following the example in Session2SecondTest.cpp. Build the code, get the test case to pass, and run the 1-hour session test: Record the timing of three runs of the test program, and note the median value. Compare the timings to others on your team. Commit your code changes with the commit command:
  5. Note that this approach to DIP requires a lot of clock classes, one for each timing sequence period.

Branch mock: Session Testing with Mock Objects

  1. Change to the branch mock. Make sure to verify that you are on the correct branch.
  2. Writing all these clocks creates a lot of classes. One workaround is to use a configurable clock class, where each clock object can have a different time period. The provided MockClock does just that. Build and run the Session2SecondTest.
  3. The test for the 5-second session in Session5SecondTest.cpp is incomplete. Complete it following the example in Session2SecondTest.cpp. Build the code, get the test case to pass, and run the 5-second session test: Commit your code changes with the commit command:
  4. The test for the 1-hour session in Session1HourTest.cpp is incomplete. Complete it following the example in Session2SecondTest.cpp. Build the code, get the test case to pass, and run the 1-hour session test: Commit your code changes with the commit command: