A library that exports value-parameterized test fixtures can't use GetParam
in its translation units
#4530
Unanswered
JimDLundgren
asked this question in
Community Help
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, at my work I've been "componentizing" one of our test suites with the goal of splitting it from one giant test executable to multiple smaller ones. As part of this, I'm moving out the fixture classes to its own library, so that it can be included/linked against from all of the new test programs. However, I'm hitting a problem for a value-parameterized test fixture - when I run the test program using the value-parameterized fixture the program aborts with the following message:
The following code reproduces it when run through my company's build system. Note, I unfortunately haven't had time to minimize the setup to run outside my company's build system (i.e. clone gtest and run the make commands myself). Happy to close this question off if that's essential to do first (I however won't likely be able to try it out in the near future though - so thought I'd still submit the question so that it's captured in case anyone else hits/has hit something similar).
To reproduce, we have one library - say libfixture - which exports the fixture.
The header exporting the class, Fixture.hpp:
with implementation in Fixture.cpp like so:
The test reside in a separate executable which links to libfixture. Code looks like:
When running the test executable, the program is aborted with the previously mentioned error message.
Note: if I move the implementation of
Fixture::SetUp
to the header file, everything does work fine. This is my current workaround, but it's not ideal if the fixture class is more complicated and callGetParam
from many places in its implementation.So, the below does work fine and doesn't produce the error:
Fixture.hpp - in library libfixture
And Fixture.cpp is now empty/removed.
The test executable that links to libfixture looks the same:
Running the test executable now works fine.
I believe the reason is the following:
Value-parameterized tests derives from following template class which stores the parameter as a static member variable:
googletest/googletest/include/gtest/gtest.h
Line 1709 in d83fee1
As this is a templated static variable, each DLL will have their own instance of it. The test executable will set the parameter as expected, but if the fixture tries to access the parameter - i.e. call
GetParam
from one of its methods (constructor/SetUp/... where it's allowed) - it will use its own instance, which is unset and hence the null-check will abort the program. At least this is what looks to be happening in my case, as per my reproduction steps it works fine if I move theGetParam
call into the header.However, I'm not fully certain if this is always a problem. From this Stack Overflow answer, it sounds like the problem happens when compiled with
-fvisibility=hidden
(which I believe we do at my company and isn't something I have control over). In this case, it could be "fixed" on the gtest side if the template class was decorated with the appropriate visibility attribute.My question is whether this can be considered a bug in gtest, or instead if it's me as the client not using the gtest library in the expected way?
I would expect this to work, but - assuming the linked stack overflow answer is correct - if gtest is not meant to be compiled with
-fvisibility=hidden
I can see this as a my problem. If the latter is the case, it'd be helpful with this documented in the gtest build instructions (and perhaps the error message updated to help in case other hits the same issue).Beta Was this translation helpful? Give feedback.
All reactions