googletest-找出在哪里发出了意外呼叫

时间:2019-02-25 16:31:22

标签: c++ unit-testing googletest googlemock

我在googletest中运行了一些单元测试。我希望对模拟函数(EXPECT_CALL(*rtosMock, xQueueGenericSend( arg , _, _, _)).Times(AtLeast(1));)进行某些调用,并为arg使用一些不同的值。我最终收到了一个意外值的电话。

unknown file: Failure

Unexpected mock function call - returning default value.
      Function call: xQueueGenericSend(NULL, 0x7fff38c99e80, 100, 0)
           Returns: 0
Google Mock tried the following 24 expectations, but none matched:

test.cpp:95: tried expectation #0: EXPECT_CALL(*rtosMock, xQueueGenericSend( arg , _, _, _))...
Expected arg #0: is equal to 0x561fede86f74
Actual: NULL
Expected: to be called at least once
Actual: called twice - satisfied and active
[...]

现在在控制台中,向我显示它期望的值和不合适的值,但不显示意外调用的位置。除了逐步执行程序外,是否有办法了解意外调用的来源(例如打印文件,行号或调用堆栈)?

1 个答案:

答案 0 :(得分:1)

摘自文档:https://github.com/google/googletest/blob/master/googlemock/docs/FrequentlyAskedQuestions.md#i-cant-figure-out-why-google-mock-thinks-my-expectations-are-not-satisfied--what-should-i-do

如果您使用--gmock_verbose=info运行测试程序,则每次调用模拟方法后,GoogleMock都会打印堆栈跟踪。但是,这可能会产生巨大的输出,尤其是对于大型测试。您可能需要使用--gtest_filter=标志或设置环境变量GTEST_FILTER将测试限制为特定测试(即失败测试)。它使用您要运行的测试的正则表达式。

您还可以在调试器中运行代码,并在xQueueGenericSend的调用上设置断点,并检查每个调用以查看何时发生故障。然后,您将能够缩小错误发生的可能范围。

相关问题