Implementing Unit Testing for the PC Teacher Desktop Application Using GoogleTest and Mocking
This article describes how the PC teacher desktop client adopts a layered MVL architecture, introduces unit testing with GoogleTest and GoogleMock, explains the underlying TEST and RUN_ALL_TESTS macro mechanisms, demonstrates mock view implementation, and shows how code coverage is measured to achieve targeted test coverage levels.
Project background: The PC teacher desktop client adopts an MVL layered architecture (View, Model, Logic) and aims to introduce unit testing to improve code quality, reduce testing pressure, and achieve at least 50% coverage for all modules and 80% for core modules.
Why unit testing: Unit tests verify that code behavior matches expectations, offering advantages over functional testing such as comprehensive edge‑case coverage, reduced reliance on manual testing resources, and driving higher code quality through developer‑focused verification.
Integrating the test framework: The project uses the googletest framework for writing test cases and googlemock for mocking non‑test interfaces, allowing exception scenarios to be constructed without modifying production code. Test code resides in a separate repository that references the main engineering code as an external dependency.
Implementation of the TEST macro: Each test case is written with the TEST(TestSuite, TestName) macro. During preprocessing the macro expands into a class derived from ::testing::Test with a generated TestBody method. Example:
int foo(int a, int b) { return a + b; }</code>
<code>TEST(Test_foo, test_normal) { EXPECT_EQ(2, foo(1, 1)); }The expansion creates a class Test_foo_test_normal_Test that registers itself via MakeAndRegisterTestInfo.
RUN_ALL_TESTS macro: The macro invokes ::testing::UnitTest::GetInstance()->Run(), which creates a singleton UnitTest object. The run sequence traverses the internal UnitTestImpl vector of TestCase objects, each containing TestInfo entries that instantiate the concrete test class, call SetUp, TestBody, and TearDown while handling exceptions.
Mocking the View layer: An interface IGiftView defines UI callbacks. A MockGiftView class inherits from the concrete view and uses MOCK_METHOD macros to create mock functions. Behavior is defined with EXPECT_CALL and WillRepeatedly(testing::Return(...)):
MockGiftView* getMockGiftView() {
MockGiftView* ptrView = new MockGiftView(nullptr);
EXPECT_CALL((*ptrView), notifyCreateConfirmDialog())
.WillRepeatedly(testing::Return(true));
// ... other expectations ...
return ptrView;
}Sample unit‑test case: The test creates a mock view, sets a false return for notifyCreateConfirmDialog to simulate user cancellation, builds mock logic and model objects, invokes the business method, and asserts the expected result:
TEST(testLogicError, HandleNoneZeroInput) {
MockGiftView* ptrView = new MockGiftView(nullptr);
EXPECT_CALL((*ptrView), notifyCreateConfirmDialog())
.WillRepeatedly(testing::Return(false));
// Initialize model, logic, and JSON data ...
bool bResult = ptrLogic->onCallCreateComplete();
EXPECT_TRUE(bResult == false);
}Coverage analysis: Using OpenCppCoverage, the project measures line‑level coverage for each module, visualizing results with screenshots. The goal is to keep overall module coverage above 50% and core module coverage above 80%.
Summary: GoogleTest provides a straightforward way to integrate unit tests into the C++ codebase, and combined with OpenCppCoverage it enables easy monitoring of test coverage. The teacher client project has adopted this approach, establishing a testing pipeline that enforces coverage thresholds and improves overall software quality.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Xueersi Online School Tech Team
The Xueersi Online School Tech Team, dedicated to innovating and promoting internet education technology.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
