googletest icon indicating copy to clipboard operation
googletest copied to clipboard

[FR]: Support explicit checking for leaked mock objects

Open blueset opened this issue 2 years ago • 0 comments
trafficstars

Does the feature exist in the most recent commit?

No.

Why do we need this feature?

This proposal is a reintroduction of #561.

Our team at Microsoft is using gMock as the mock library for our C++ unit tests. There are cases in our unit test code where some mock objects assigned to global pointers are not properly deconstructed, and could randomly affect subsequent test cases depending on the run order.

While gMock ships with a mock object leak check at the end of the process, it is hard to narrow down where the leak has happened when a large number of test cases are running in the same process.

Describe the proposal.

The proposal is to support explicit checking for leaked mock objects by adding a method bool Mock::CatchLeakedMocks() that has a similar behavior of MockObjectRegistry::~MockObjectRegistry().

Quoting from #561:

// Returns true if the mock registry is empty; otherwise
// generates a non-fatal failure for each leaked mock object
// and returns false.
bool Mock::CatchLeakedMocks()

With this method, we can check specifically at the conclusion of each test to see if there is any leaked objects, helping narrowing down the scope for debugging.

Is the feature specific to an operating system, compiler, or build system version?

No.

cc @mjber

blueset avatar Apr 12 '23 01:04 blueset