1

I'm learning GTest in Visual Studio 2022. I tried to run the code below but I confront the error "function definition for TYPED_TEST_SUITE not found". Unfortunately I couldn't find any appropriate answer for my problem. I'll be glad if you help me to solve it.

#include <vector>
#include "pch.h"
#include "gtest/gtest.h"

// An abstract interface
class MyLib {
public:
    virtual ~MyLib() {}
    // Finds the max element in a vector
    virtual int FindMax(const std::vector<int>& inputs) const = 0;
};
//-----------------------------------------------------------------------------
// Iterative implementation.
class MyLibIterative : public MyLib {
public:
    // Finds the max element in a vector
    virtual int FindMax(const std::vector<int>& inputs) const override {
        if (inputs.size() == 0) {
            return -1;
        }

        int result = std::numeric_limits<int>::min();
        for (auto n : inputs) {
            if (n > result) {
                result = n;
            }
        }
        return result;
    }
};
//-----------------------------------------------------------------------------
// Recursive implementation.
class MyLibRecursive : public MyLib {
public:
    // Finds the max element in a vector

    virtual int FindMax(const std::vector<int>& inputs) const override {
        if (inputs.size() == 0) {
            return -1;
        }
        return FindMaxRecursiveAux(inputs, 0, inputs.size() - 1);
    }

private:
    int FindMaxRecursiveAux(const std::vector<int>& inputs, int left,
        int right) const {
        if (left >= right) {
            return inputs[left];
        }
        int mid = (right + left) / 2;
        return std::max(FindMaxRecursiveAux(inputs, left, mid),
            FindMaxRecursiveAux(inputs, mid + 1, right));
    }
};
//-----------------------------------------------------------------------------
// A test fixture class template.
template <class T>
class MyLibTest : public testing::Test {
public:
    T lib_;
};

using testing::Types;

// The list of types we want to test.
typedef Types<MyLibIterative, MyLibRecursive> Implementations;

// Then use TYPED_TEST(TestCaseName, TestName) to define a typed test,
TYPED_TEST_SUITE(MyLibTest, Implementations);

// similar to TEST_F.
// Since we are in the template world, C++ requires explicitly
// writing 'this->' when referring to members of the fixture class.
// This is something you have to learn to live with.

TYPED_TEST(MyLibTest, FindMaxHandlesEmpty) {
    std::vector<int> inputs = {};
    EXPECT_EQ(this->lib_.FindMax(inputs), -1);
}

TYPED_TEST(MyLibTest, FindMaxHandlesPositiveSizeOne) {
    std::vector<int> inputs = { 2 };
    EXPECT_EQ(this->lib_.FindMax(inputs), 2);
}

TYPED_TEST(MyLibTest, FindMaxHandlesConsecutiveNumbers) {
    std::vector<int> inputs = { 1, 2, 3, 4 };
    EXPECT_EQ(this->lib_.FindMax(inputs), 4);
}

TYPED_TEST(MyLibTest, FindMaxHandlesNonConsecutiveNumbers) {
    std::vector<int> inputs = { 1, 12, 8, 22, 2, 18, 3, 1, 4 };
    EXPECT_EQ(this->lib_.FindMax(inputs), 22);
}

TYPED_TEST(MyLibTest, FindMaxHandlesAllDuplicateNumbers) {
    std::vector<int> inputs = { 1, 1, 1, 1, 1, 1 };
    EXPECT_EQ(this->lib_.FindMax(inputs), 1);
}

As you can see it is not just auto compilation false warning.

  • This is not a compiler error, this is Intellisense, auto-completion false warning. See similar questions https://stackoverflow.com/questions/44314505/typed-tests-with-google-test-show-a-warning-in-visual-studio-2017, https://stackoverflow.com/questions/56055566/visual-studio-2017-warning-for-instantiate-test-suite-p-while-using-googletest – 273K Sep 03 '22 at 11:47
  • This code compiles correctly (See here: https://godbolt.org/z/j6dGfMfM1), as the other comment mentions, the error you see is probably just due to Intellisense. Are you not able to run it? – Ari Sep 05 '22 at 18:05
  • I am seeing the same issue for TYPED_TESTS. Do you plan to file a bug report for Visual Studio? – 20knots Sep 07 '22 at 07:54

0 Answers0