-1

I have a template test class which I am looking to write test fixtures for. I have the following snippet which works but it doesn't "group" the test fixtures together because of the template type so I have to run each of them separately rather than just once (i.e UTestClass). When I run UTestClass, it says No tests were found

Note that the snippet below allows accessing of member variable _value across different test fixtures too so I'd want to have this behavior.

Is there a way to group the template test fixtures together? Perhaps in a test suite or another class which allows each test to run ONLY with a specified type?

template<typename T>
struct UTestClass : public testing::Test
{
   T _value;
};

using Float = UTestClass<float>;
using Double = UTestClass<double>;

TEST_F(Float, TestA)
{
  // able to access _value
}

TEST_F(Double, TestB)
{
  // able to access _value
}
xyf
  • 664
  • 1
  • 6
  • 16
  • Strictly from filtering perspective, `--gtest_filter="Float.*:Double.*"` will run both of these in one run of the program. Or you can discard those aliases, with `TEST_F(UTestClass, TestA)` and `TEST_F(UTestClass, TestA)` should work with filter `UTestClass*`. Not sure if that's what you are asking about tho. – Yksisarvinen Feb 09 '23 at 17:47
  • I remember @xyf is that guy that does not read manuals and persistently asks questions on SO. – 273K Feb 09 '23 at 17:49
  • @Yksisarvinen I don't plan on using gtest filter; I am using CLion which allows me to run ALL the tests fixture as long as they're associated with `UTestClass` in one-shot. With what I have currently, it only allows to run test fixtures separately – xyf Feb 09 '23 at 21:03

1 Answers1

0

TYPED_TEST_SUITE and TYPED_TEST are for such purposes.

template<typename T>
struct UTestClass : public testing::Test {};

using UTestClassTypes = ::testing::Types<float, double>;
TYPED_TEST_SUITE(UTestClass, UTestClassTypes);

TYPED_TEST(UTestClass, TestA) {
}
273K
  • 29,503
  • 10
  • 41
  • 64
  • Would I be able to access the members of `UTestClass` within each TYPED_TEST though? Ideally I want something like as if it's not "templated". If I have `TEST_F(UTestClass, TestA)`, I'd be able to access the class members inside it – xyf Feb 09 '23 at 17:52
  • If you have another question, ask it separately with a [mcve]. The shown code in the question does not have any class member. – 273K Feb 09 '23 at 17:53
  • it's not a separate question. The snippet I have in the description allows the accessibility of struct members across the text fixtures which your code doesn't seem to. – xyf Feb 09 '23 at 17:55
  • You have edited the question after the answer was posted and you blame me in miss of the updated part! – 273K Feb 09 '23 at 17:58
  • I thought it was implicit? The behavior of your answer didn't match my original snippet even. Apologies for the confusion – xyf Feb 09 '23 at 17:59
  • You are lazy guy! `TYPED_TEST(UTestClass, TestA) { this->_value = 0.0; }` works perfectly as any C++ template code. – 273K Feb 09 '23 at 18:02
  • Please stop being personal. I am here trying to understand a few things here. Why `this` is needed? cause it's template? – xyf Feb 09 '23 at 18:05
  • You again have yet another basic question inside a single question post, that confirms your laziness. Use any book of [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) for extending your C++ knowledge. – 273K Feb 09 '23 at 18:08