Learn C++ Online

Test-driven development

On this page
This lesson contains approximately 45 minutes of video content.

Unit tests

Writing unit tests in C++ with the Catch2 framework

We will use the Catch2 framework unit testing framework in our class. To learn how to compose unit tests using this framework, please see the Catch2 framework for authoring test cases page under our site's Resources section. You will be required to write test cases using this framework for assessments in this course. Therefore, you can either become familiar with it today or once the first assignment requiring it is released.

Coverage measurement

Test-driven development

Beyond correctness

Questions

Q1

QTDD Q1

This question is ungraded.

What are unit tests? Select the best answer.

Q2

QTDD Q2

This question is ungraded.

A given code contains an if-else statement. The test suite for this portion of code contains two tests:

  • one that traverses the if branch, and
  • one that traverses the else branch.
What coverage level is satisfied by this test suite?

Q3

QTDD Q3

This question is ungraded.

What kind(s) of tests with 100% coverage, guarantee bug-free code:

  1. C0 (statement coverage)
  2. C1 (conditionals coverage)
  3. C2 (paths coverage)
  4. DU-coverage (all pairs of define X/use X for every variable X)
Select the best answer.

Q4

QTDD Q4

This question is ungraded.

Which of the following is NOT true? Select the best answer.

Q5

QTDD Q5

This question is ungraded.

Why are boolean arguments a yellow flag? Select the best answer.

Q6

QTDD Q6

This question is ungraded.

What should you do if arguments "travel in a pack"?

Q7

QTDD Q7

This question is ungraded.

The two pieces of code that follow have the same logical functionality but different structure. Which statement about them is TRUE?

Version 1.

void foo(bool x, bool y) {
  if (x) {
    if (y) z();    
  }      
  else {       
    if (y) w();    
  }      
}     

Version 2.

void check_y_z(bool val);
void check_y_w(bool val);

void def foo(bool x, bool y) {              
  if (x) {
    check_y_z(y);
  }
  else {
    check_y_w(y);                     
  }
}

void check_y_z(bool val) {
  if (val) z();
}

void check_y_w(bool val) {
  if (val) w(); 
}