I have some unit tests written on Windows (Visual Studio 2017) and I need to port them on Linux (GCC 4.9.2 - I'm stuck with this version...). I've come with a simple example for my problem which compiles fine on Windows (I don't think it should compile as MyParamType
is a dependent type from e template base class) and doesn't compile on Linux.
Example:
#include <gtest/gtest.h>
template<typename T>
struct MyTest : public testing::Test
{
using MyParamType = T;
};
using MyTypes = testing::Types<int, float>;
TYPED_TEST_CASE(MyTest, MyTypes);
TYPED_TEST(MyTest, MyTestName)
{
MyParamType param;
}
In member function ‘virtual void MyTest_MyTestName_Test::TestBody()’:error: ‘MyParamType’ was not declared in this scope MyParamType param;
By changing to:
TYPED_TEST(MyTest, MyTestName)
{
typename MyTest<gtest_TypeParam_>::MyParamType param;
}
The code compiles, but it looks very ugly.
Is there an easy/nice way to get the template parameter type from a TYPED_TEST
?
Use your favorite command-line-parsing technique, store the results in some global variable, and refer to it during your tests. If a command-line options looks like a Google Test option but really isn't, then the program will print its help message and exit without running any tests.
TYPED_TEST(TestSuiteName, TestName) { ... statements ... } Defines an individual typed test named TestName in the typed test suite TestSuiteName . The test suite must be defined with TYPED_TEST_SUITE .
TEST_F() is useful when you need access to objects and subroutines in the unit test. Example test. TEST_P() is useful when you want to write tests with a parameter.
h. The directory that contains gtest is not in your list of include search paths. gtest/gtest. h is working only because it's relative to the current file. You need to add the path that contains the directory gtest to your list of include search paths.
The answer is hidden in the docs:
#include <gtest/gtest.h>
template<typename T>
struct MyTest : public testing::Test
{
using MyParamType = T;
};
using MyTypes = testing::Types<int, float>;
TYPED_TEST_CASE(MyTest, MyTypes);
TYPED_TEST(MyTest, MyTestName)
{
// To refer to typedefs in the fixture, add the 'typename TestFixture::'
// prefix. The 'typename' is required to satisfy the compiler.
using MyParamType = typename TestFixture::MyParamType;
}
https://github.com/google/googletest/blob/master/googletest/docs/advanced.md
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With