Mixing Type- and Value-Parameterized Tests in same Test Class
- Dominant language
- C++
- Stars
- 39.5k
- Forks
- 10.9k
- Avg merge
- 6d 13h
- Merged PRs (30d)
- 1
Description
I try to write a test class which is both type- and value-parameterized.
Is this possible at the moment?
I cannot find a, instantiation macro for both a type- and value-parameterized tests.
Here is [ simple example](https://github.com/S3ler/GoogleTestTypeValueParameterizedTests/blob/master/TypeValueParameterizedFooTest.cpp) I tried to implement:
```C++
#include
#include
template
class TypeValueParameterizedFooTest : public ::testing::Test, ::testing::WithParamInterface {
public:
virtual void SetUp() { }
virtual void TearDown() { }
TypeValueParameterizedFooTest() { }
virtual ~TypeValueParameterizedFooTest() { }
typedef std::list List;
static A shared_;
A value_;
};
TYPED_TEST_SUITE_P(TypeValueParameterizedFooTest);
TYPED_TEST_P(TypeValueParameterizedFooTest, TypeValueParameterizedDoesBlah) {
TypeParam n = this->value_;
// n += TestFixture::shared_;
typename TestFixture::List values;
values.push_back(n);
std::cout << "Example TypeValueParameterizedFooTest Test Param: " << std::to_string(GetParam()) << std::endl;
}
REGISTER_TYPED_TEST_SUITE_P(TypeValueParameterizedFooTest, TypeValueParameterizedDoesBlah);
typedef ::testing::Types MyTypeValueParameterizedTypes;
// INSTANTIATE_TYPED_TEST_SUITE_P is obviusly not the right macro
INSTANTIATE_TYPED_TEST_SUITE_P(MyTypeValueParameterizedTestSuite,
TypeValueParameterizedFooTest,
MyTypeValueParameterizedTypes,
::testing::Values(1, 2, 3));
```
And of course I cannot compile it:
```
/home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp: In member function ‘virtual void gtest_suite_TypeValueParameterizedFooTest_::TypeValueParameterizedDoesBlah::TestBody()’:
/home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp:44:89: error: there are no arguments to ‘GetParam’ that depend on a template parameter, so a declaration of ‘GetParam’ must be available [-fpermissive]
std::cout << "Example TypeValueParameterizedFooTest Test Param: " << std::to_string(GetParam()) << std::endl;
^~~~~~~~
/home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp:44:89: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
In file included from /home/GoogleTestTypeValueParameterizedTests/cmake-build-debug/googletest-src/googletest/include/gtest/gtest.h:69:0,
from /home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp:9:
/home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp: At global scope:
/home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp:53:49: error: temporary of non-literal type ‘testing::internal::ValueArray’ in a constant expression
::testing::Values(1, 2, 3));
~~~~~~~~~~~~~~~~~^~~~~~~~~
In file included from /home/GoogleTestTypeValueParameterizedTests/cmake-build-debug/googletest-src/googletest/include/gtest/gtest-param-test.h:184:0,
from /home/GoogleTestTypeValueParameterizedTests/cmake-build-debug/googletest-src/googletest/include/gtest/gtest.h:65,
from /home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp:9:
/home/GoogleTestTypeValueParameterizedTests/cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-param-util.h:728:7: note: ‘testing::internal::ValueArray’ is not literal because:
class ValueArray {
^~~~~~~~~~
/home/GoogleTestTypeValueParameterizedTests/cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-param-util.h:728:7: note: ‘testing::internal::ValueArray’ is not an aggregate, does not have a trivial default constructor, and has no constexpr constructor that is not a copy or move constructor
In file included from /home/GoogleTestTypeValueParameterizedTests/cmake-build-debug/googletest-src/googletest/include/gtest/gtest.h:69:0,
from /home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp:9:
/home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp:50:1: error: type/value mismatch at argument 1 in template parameter list for ‘template struct testing::internal::NameGeneratorSelector’
INSTANTIATE_TYPED_TEST_SUITE_P(MyTypeValueParameterizedTestSuite,
^
/home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp:50:1: note: expected a type, got ‘testing::Values(1, 2, 3)’
/home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp:50:1: error: no matching function for call to ‘GenerateNames<, testing::internal::TypeList >::type>()’
INSTANTIATE_TYPED_TEST_SUITE_P(MyTypeValueParameterizedTestSuite,
^
In file included from /home/GoogleTestTypeValueParameterizedTests/cmake-build-debug/googletest-src/googletest/include/gtest/gtest.h:60:0,
from /home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp:9:
/home/GoogleTestTypeValueParameterizedTests/cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-internal.h:703:26: note: candidate: template std::vector > testing::internal::GenerateNames()
std::vector GenerateNames() {
^~~~~~~~~~~~~
/home/GoogleTestTypeValueParameterizedTests/cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-internal.h:703:26: note: template argument deduction/substitution failed:
In file included from /home/GoogleTestTypeValueParameterizedTests/cmake-build-debug/googletest-src/googletest/include/gtest/gtest.h:69:0,
from /home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp:9:
/home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp:50:1: error: template argument 1 is invalid
INSTANTIATE_TYPED_TEST_SUITE_P(MyTypeValueParameterizedTestSuite,
^
/home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp: In instantiation of ‘void gtest_suite_TypeValueParameterizedFooTest_::TypeValueParameterizedDoesBlah::TestBody() [with gtest_TypeParam_ = unsigned int]’:
/home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp:53:59: required from here
/home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp:44:97: error: ‘static const ParamType& testing::WithParamInterface::GetParam() [with T = int; testing::WithParamInterface::ParamType = int]’ is inaccessible within this context
std::cout << "Example TypeValueParameterizedFooTest Test Param: " << std::to_string(GetParam()) << std::endl;
~~~~~~~~^~
In file included from /home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp:9:0:
/home/GoogleTestTypeValueParameterizedTests/cmake-build-debug/googletest-src/googletest/include/gtest/gtest.h:1882:27: note: declared here
static const ParamType& GetParam() {
^~~~~~~~
/home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp: In instantiation of ‘void gtest_suite_TypeValueParameterizedFooTest_::TypeValueParameterizedDoesBlah::TestBody() [with gtest_TypeParam_ = int]’:
/home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp:53:59: required from here
/home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp:44:97: error: ‘static const ParamType& testing::WithParamInterface::GetParam() [with T = int; testing::WithParamInterface::ParamType = int]’ is inaccessible within this context
std::cout << "Example TypeValueParameterizedFooTest Test Param: " << std::to_string(GetParam()) << std::endl;
~~~~~~~~^~
In file included from /home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp:9:0:
/home/GoogleTestTypeValueParameterizedTests/cmake-build-debug/googletest-src/googletest/include/gtest/gtest.h:1882:27: note: declared here
static const ParamType& GetParam() {
^~~~~~~~
/home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp: In instantiation of ‘void gtest_suite_TypeValueParameterizedFooTest_::TypeValueParameterizedDoesBlah::TestBody() [with gtest_TypeParam_ = char]’:
/home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp:53:59: required from here
/home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp:44:97: error: ‘static const ParamType& testing::WithParamInterface::GetParam() [with T = int; testing::WithParamInterface::ParamType = int]’ is inaccessible within this context
std::cout << "Example TypeValueParameterizedFooTest Test Param: " << std::to_string(GetParam()) << std::endl;
~~~~~~~~^~
In file included from /home/GoogleTestTypeValueParameterizedTests/TypeValueParameterizedFooTest.cpp:9:0:
/home/GoogleTestTypeValueParameterizedTests/cmake-build-debug/googletest-src/googletest/include/gtest/gtest.h:1882:27: note: declared here
static const ParamType& GetParam() {
^~~~~~~~
```
Is there a way to instantiate such a test class or is there a workaround?
Contributor guide
Assessment
This issue has not been assessed yet.