2014-05-19 2 views
1

gtest를 사용하여 클래스를 테스트하는 모든 public 메소드를 테스트해야한다고 들었습니다. 그러나 개인/보호의 가치를 변화시키는 세터 (setter) 방법은 어떻습니까? 어떻게 테스트해야합니까? 아래 예.gtest에서 setter 메소드를 테스트하는 방법은 무엇입니까?

class Formatter 
{ 
public: 
    Formatter(); 
    void setFormat(std::string format) 
    { 
     formatPattern = format; 
    } 
    std::string format(ExampleObject objectToFormat) 
    { 
     //do something with objectToFormat using protected formatPattern 
     //and put output to std::string retval 
     return retval; 
    } 

protected: 
    std::string formatPattern; 
}; 

EDIT : format() 메소드가 추가되었습니다.

+0

당신이 얻을 전화 값을 확인은, 당신은 설정 호출과 다른 시간에 얻을 그 값을 비교. 값이 예상대로 변경되면 작동합니다. – Theolodis

+0

하지만 (내 예제처럼) getter 메소드가 없다면 어떻게 될까요? – Nowax

+0

당신은 단지 하나를 씁니까? 어쨌든 그것을 사용하지 않는다면 내부 사적인 변수의 목적은 무엇입니까? 그리고 만약 당신이 그것을 사용한다면 그것은 예상 된 변화가 일어 났는지를 확인할 수 있도록 아마도 어떤 결과를 바꿀 것입니다. – Theolodis

답변

2

내 문제에 대한 답변을 찾았습니다. 테스트하기 전에 나는 formatter로부터 상속받은 새로운 클래스를 생성한다. 그런 다음이 클래스에서는 부모 클래스에서 공용 설정 메서드를 테스트하는 데 필요한 getter 메서드를 구현합니다.

namespace consts { 
    std::string simpleFormatPattern = "@name @severity @message"; 
    std::string formatterOutput = "error ERROR Here is some message"; 
    LogEntry logEntry("error","Here is some message", ERROR); 
} //namespace consts 

class ut_formatter : public Formatter 
{ 
public: 
    ut_formatter() 
    {} 
    ~ut_formatter() 
    {} 

    std::string getFormatPattern(void) 
    { 
     return formatPattern; 
    } 
}; 

TEST(ut_formatter, SetFormatOk) 
{ 
    ut_formatter formatter; 
    formatter.setFormat(consts::simpleFormatPattern); 

    ASSERT_EQ(consts::simpleFormatPattern, formatter.getFormatPattern()); 
} 

TEST(ut_formatter, FormatOk) 
{ 
    ut_formatter formatter; 
    formatter.setFormat(consts::simpleFormatPattern); 

    ASSERT_EQ(consts::formatterOutput, formatter.format(consts::logEntry)); 
} 
1

개인/보호 된 변수를 설정할 때 발생할 수있는 영향을 테스트합니다. 예를 들어 :

이 예에서
std::ostringstream a, b; 
a << 32; 
ASSERT_EQ(a.str(),"32"); 
b << std::hex() << 32; 
ASSERT_EQ(b.str(),"20"); 

, '표준 : 진수()'는 .str() 출력에 볼 수있는 ostringstream, 일부 내부 서식을 설정합니다.

+0

예제에서는 getter 메서드 (str())를 사용하고 제 예제에서는 (내 문제인 경우에도) 하나도 없습니다. – Nowax

+0

@ user3148625 귀하의 경우,'format' 호출 결과를 확인하십시오. (예를 들어,'setFormat'를 호출하고,'format'를 호출 한 다음 결과가'setFormat'에서 앞서 지정한 값과 일치하는지 확인하십시오.) – Lilshieste

0

뷰라고 할 것이 많은이 당신 해야하지 단위 테스트 하는 API의 내부, 단지 대중 행동. 헤더 gtest/gtest_prod.h에 정의되어

FRIEND_TEST(TestCaseName, TestName) 

: 당신은 어쨌든 을하기로 결정하면, googletest 방법 우리는 매크로를 사용할 수 있습니다.

Formatter.h

#include "gtest/gtest_prod.h" 
#include <string> 

class Formatter 
{ 
    FRIEND_TEST(t_Formatter_setFormat, t_formatPatternSetCorrect); 
public: 
    Formatter(){}; 
    void setFormat(std::string format) 
    { 
     formatPattern = format; 
    } 
    // Methods, methods... 

protected: 
    std::string formatPattern; 
}; 

테스트 러너

#include "Formatter.h" 
#include "gtest/gtest.h" 
#include <string> 

TEST(t_Formatter_setFormat, t_formatPatternSetCorrect) { 
    Formatter f; 
    f.setFormat("A%Format%Pattern"); 
    EXPECT_EQ(f.formatPattern,"A%Format%Pattern"); 
} 

int main(int argc, char **argv) 
{ 
    ::testing::InitGoogleTest(&argc, argv); 
    return RUN_ALL_TESTS(); 
} 

당신이 생각 하듯이,

: 여기

는 클래스 Formatter이 적용 예제 테스트 주자
FRIEND_TEST(t_Formatter_setFormat, t_formatPatternSetCorrect); 

은 테스트를 방금 Formatter 클래스의 친구로 만들어서 보호 된 및 비공개 멤버에 액세스 할 수 있습니다.

여기에 표시된 해결책은 에 gtest_prod.h을 넣고 Formatter.h에 넣어야한다는 의미에서 방해가됩니다. 그러나 gtest_prod.h 그 자체는 입니다. 여기에 the code 입니다. 에 의존하지 않고이 헤더를 소프트웨어 배포에 포함 할 수 있습니다. googletest를 전체 배포본에 묶을 필요가 없습니다.

Further reading

관련 문제