Unit Testing & Code Quality Flashcards
7 cards from real CPP practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Unit Testing & Code Quality flashcards as text
When using Catch2, which keyword block defines a subsection within a test case that shares outer setup but has independent state?
Answer: SECTION
In Catch2, SECTION blocks re-run the enclosing TEST_CASE setup independently for each section, giving each its own clean state.
What is a 'stub' in unit testing terminology?
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.
In Google Mock, which macro sets an expectation that a method is called at least once?
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.
What does the cppcheck static analysis tool primarily report?
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.
Which principle states that a unit test should test only one logical concept per test case?
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.
What is the purpose of the `-fsanitize=undefined` compiler flag in GCC/Clang?
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.
In the AAA (Arrange-Act-Assert) pattern for writing unit tests, what happens in the 'Arrange' phase?
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.