Re: [googletest] Can't use TestWithParam and SetUpTestCase/TearDownTestCase together

2,408 views
Skip to first unread message

Vlad Losev

unread,
Nov 12, 2012, 6:27:19 PM11/12/12
to Google C++ Testing Framework
You've made SetUpTestCase/TearDownTestCase protected, and Google Test's code cannot access them. They should be declared public. 

On Fri, Nov 9, 2012 at 2:00 AM, Andrey Zelenchuk <soa...@gmail.com> wrote:
Hello. I have an issue.

Code:
//----------------------------------------------------------------------
#include "gtest/gtest.h"

class FooTest: public ::testing::TestWithParam<std::string>
{
  protected:
    static void SetUpTestCase() {
      shared_resource = new int;
      *shared_resource = 7;
    }

    static void TearDownTestCase() {
      delete shared_resource;
      shared_resource = NULL;
    }

    static int* shared_resource;
};

int* FooTest::shared_resource = NULL;

TEST_P(FooTest, Test1)
{
    const std::string &param = this->GetParam();
    int x = *shared_resource;
}

INSTANTIATE_TEST_CASE_P(FooTestInstantiation, FooTest, ::testing::Values("p1", "p2", "p3"));
//----------------------------------------------------------------------

Result:
Error 1 error C2248: 'FooTest::TearDownTestCase' : cannot access protected member declared in class 'FooTest' P:\cpp-libs\VC9\x86\gtest-1.6.0-vc9-x86\include\gtest\internal\gtest-param-util.h 516 tests
Error 2 error C2248: 'FooTest::SetUpTestCase' : cannot access protected member declared in class 'FooTest' P:\cpp-libs\VC9\x86\gtest-1.6.0-vc9-x86\include\gtest\internal\gtest-param-util.h 516 tests

Expected: successfull compilation.

Version of Google Test: gtest-1.6.0-vc9-x86
OS: Windows 7 Professional SP1 x64.

What to do? Could anyone help me?

Andrey Zelenchuk

unread,
Nov 16, 2012, 12:38:45 AM11/16/12
to googletes...@googlegroups.com
Thank you! It works.
But it is not obvious, because AdvancedGuide says (in the example) that it should be declared protected, and protected declaration works with ::testing::Test but doesn't work with ::testing::TestWithParam.

вторник, 13 ноября 2012 г., 6:28:01 UTC+7 пользователь Vlad Losev написал:
Reply all
Reply to author
Forward
0 new messages