CPP Unit Testing & Code Quality 3 โ Questions and Answers
Question 1: When using Catch2, which keyword block defines a subsection within a test case that shares outer setup but has independent state?
- GIVEN
- SECTION (Correct answer)
- WHEN
- AND_THEN
Correct answer: SECTION
In Catch2, SECTION blocks re-run the enclosing TEST_CASE setup independently for each section, giving each its own clean state.
Question 2: What is a 'stub' in unit testing terminology?
- A test that always passes to skip a feature
- A replacement for a dependency that returns canned responses without verifying calls (Correct answer)
- A tool that measures test execution time
- A compiler pragma disabling warnings in test code
Correct answer: A replacement for a dependency that returns canned responses without verifying calls
A stub replaces a real dependency and provides hardcoded return values to control the system under test's behavior, without asserting how it was called.
Question 3: In Google Mock, which macro sets an expectation that a method is called at least once?
- ON_CALL
- EXPECT_CALL(...).Times(AtLeast(1)) (Correct answer)
- MOCK_METHOD
- EXPECT_CALL(...).Times(Exactly(1))
Correct answer: EXPECT_CALL(...).Times(AtLeast(1))
EXPECT_CALL with .Times(AtLeast(1)) from the testing::AtLeast matcher asserts the mocked method is invoked one or more times.
Question 4: What does the cppcheck static analysis tool primarily report?
- Style and formatting violations only
- Bugs, undefined behavior, and dangerous coding patterns without requiring compilation (Correct answer)
- Runtime performance bottlenecks
- Linker symbol conflicts
Correct answer: Bugs, undefined behavior, and dangerous coding patterns without requiring compilation
cppcheck performs static analysis on C++ source without compiling it, detecting bugs like null pointer dereferences, out-of-bounds access, and resource leaks.
Question 5: Which principle states that a unit test should test only one logical concept per test case?
- DRY (Don't Repeat Yourself)
- Single Responsibility Principle for tests (Correct answer)
- SOLID Open/Closed Principle
- YAGNI (You Aren't Gonna Need It)
Correct answer: Single Responsibility Principle for tests
Applying the Single Responsibility Principle to tests means each test case validates exactly one behavior, making failures immediately pinpoint the broken logic.
Question 6: What is the purpose of the `-fsanitize=undefined` compiler flag in GCC/Clang?
- Enables memory leak detection
- Instruments the binary to trap undefined behavior at runtime (UBSan) (Correct answer)
- Activates thread safety analysis
- Enables profile-guided optimization
Correct answer: Instruments the binary to trap undefined behavior at runtime (UBSan)
The UndefinedBehaviorSanitizer (UBSan) flag instruments the program to abort or report signed integer overflow, null dereference, and other C++ undefined behavior at runtime.
Question 7: In the AAA (Arrange-Act-Assert) pattern for writing unit tests, what happens in the 'Arrange' phase?
- The expected output is compared to actual output
- Test inputs, dependencies, and preconditions are set up (Correct answer)
- The function or method under test is invoked
- Test results are logged to a file
Correct answer: Test inputs, dependencies, and preconditions are set up
The Arrange phase initializes objects, configures mocks, and prepares all necessary inputs so the system under test starts in the desired state.
When using Catch2, which keyword block defines a subsection within a test case that shares outer setup but has independent state?