2017-01-08 1 views
2

달성하고자하는 것은 Paramaterized Test TEST_P(MyFixtureClass, DoStuff)이며 다른 값을 테스트 할 수 있습니다. 상기 값은 일반적으로 INSTANTIATE_TEST_CASE_P에 전달 된 것과 같이 상수가 아니어야합니다. 또한, 나는 다른 조명기 클래스 내에서 이상적으로 값을 사용하고 싶습니다.Google 테스트 (fixesture member value)를 사용하여 매개 변수화 된 테스트를 실행하는 방법은 무엇입니까?

매개 변수가있는 테스트를 만들 때 정적 값 대신 필드를 사용하는 방법은 없습니다. official documentation은 슬프게도 이것을 다루지 않습니다.

매개 변수화 된기구, MyFixture :

struct MyFixture : OtherFixture, ::testing::WithParamInterface<float> 
{ 
    float a; 

    void SetUp() override 
    { 
     a = GetParam(); 
    } 
}; 

OtherFixture이 같을 것이다 :이 문제의 XY-문제를 피하고 그러나


, 여기에 해당하는 의사 코드

struct OtherFixture : testing::Test 
{ 
    float a; 
    float b; 
    float c; 

    void SetUp() override 
    { 
     a = CalculateSomeFloat(); 
     b = CalculateSomeFloat(); 
     c = CalculateSomeFloat(); 
    } 
}; 

테스트 케이스가 E :

// This here is the key aspect. 
// Basically, I do not want to write a bunch of tests for a, b and c. 
// Rather, I'd just test all 3 with this one. 
TEST_P(MyFixture, DoStuff) 
{ 
    ...bunch of tests 
} 

그리고 마지막으로, 우리가 인스턴스화 것이다 파라미터 테스트 :

INSTANTIATE_TEST_CASE_P(MyFloatTesting, MyFixture, ::testing::Values(
    OtherFixture::a, OtherFixture::b, OtherFixture::c 
)); 

물론, OtherFixture::a는 부적절하다,하지만 난 상속 고정 클래스 내에서 필드를 참조 할 것 곳은 보여 (또는 그 문제에 대한 모든 조명기 클래스).


그래서 이것을 gtest로 구현할 수있는 방법이 있습니까? 반드시 매개 변수화 된 테스트를 사용할 필요는 없습니다. 동일한 테스트를 작성하는 것을 피하기 만하면 다른 객체에 대해 나에게 도움이됩니다.


모든 의견을 보내 주시면 감사하겠습니다.

답변

2

::testing::Combine을 사용해야한다고 생각합니다.

매개 변수를 float에서 std::tuple<float, float OtherFixture::*>으로 변경하십시오.

const auto membersToTest = testing::Values(
    &OtherFixture::a, 
    &OtherFixture::b, 
    &OtherFixture::c 
); 

const auto floatValuesToTest = testing::Values(
    2.1, 
    3.2 
    // ... 
); 

INSTANTIATE_TEST_CASE_P(AllMembers, 
         MyFixture, 
         testing::Combine(floatValuesToTest, membersToTest)); 

는 그런 다음 OtherFixture의 구성원에 대한 일반적인 테스트를 작성할 수 있습니다 : 매개 변수가이 방법을 사용

using OtherFixtureMemberAndValue = std::tuple<float, float OtherFixture::*>; 

struct MyFixture : OtherFixture, ::testing::WithParamInterface<OtherFixtureMemberAndValue> 
{ 
    float a = std::get<0>(GetParam()); 
    auto& memberToTest() 
    { 
     return this->*std::get<1>(GetParam()); 
    } 


}; 

는 세트를 정의하려면

TEST_P(MyFixture, test) 
{ 
    ASSERT_EQ(a, memberToTest()); 
} 

나는 또한 것이라고 조언 당신을 에 float OtherFixture::*을 작성했습니다.

이와

(210)은 실패의 경우에 좋은 메시지가 :

[FAILED] AllMembers/MyFixture.test/5 GetParam() = (3.불쾌한 의미 메시지에 비해 2 & OtherFixture :: c)

/O PrintTo w :

[FAILED] AllMembers/MyFixture.test/5 GetParam() = (3.2 같은 그것은 내가 함수 포인터를 사용할 수 나에게 발생하지 않았다 4 바이트 객체 < 10-00 00-00>)

+1

,'OtherFixture을 떠 :: *'. 그리고 나는'PrintTo' 추가에 대해 매우 감사 드리며, 추가 마일로 가셔서 감사드립니다. – Nikita

관련 문제