2011-08-24 2 views
3

내부에 타이머 기능이있는 프로그램에 google 테스트를 사용하고 싶습니다. 타이머는 Windows SetTimer()에 의해 구현되며 타임 아웃 메시지를 처리하기 위해 main()에 메시지 큐가 있습니다.메시지 대기열을 기반으로하는 Windows 애플리케이션에서 Google 테스트를 어떻게 사용할 수 있습니까?

while (GetMessage(&msg, NULL, 0, 0)) { 
    if (msg.message == WM_TIMER) { 
     ... 
    } 
    DispatchMessage(&msg); 
} 

Google 테스트의 경우 RUN_ALL_TESTS()를 호출하여 테스트를 시작합니다.

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

제 질문은이 두 부분을 어떻게 통합 할 수 있습니까? 내 코드의 일부 기능은 메시지를 보낼 것이므로이를 처리하기 위해 동일한 메시지 큐 메커니즘을 사용해야합니다.

그렇다면 각 테스트 케이스에서 메시지 큐 처리를 작성해야합니까? 그것은 실행 가능한 방법입니까?

TEST() 
{ 
    ... message queue here ... 
} 

이러한 종류의 테스트를 수행하는 적절한 방법이 있습니까? 감사합니다.

답변

1

테스트하려는 코드가 메시지 대기열 메커니즘에 종속되어있는 것으로 보입니다.

class MessageHandler 
{ 
    virtual void DispatchMessage(Msg messageToBeDispatched) = 0; 
} 

지금 당신이 productiv과 테스트의 목적을 위해 다른 메시지 핸들러를 구현할 수 있습니다 : 당신이 메시지를 보낼 필요가 모든 클래스에 주입됩니다 같은 추상적 인 메시지 핸들러를 구현하고있는 경우, 테스트 용이성을 향상시킬 수

class TestMessageHandler : public MessageHandler 
{ 
    void DispatchMessage(Msg messageToBeDispatched) 
    { 
    // Just testing, do nothing with this message, or just cout... 
    } 
} 

class ProductiveMessageHandler : public MessageHandler 
{ 
    void DispatchMessage(Msg messageToBeDispatched) 
    { 
    // Now do the real thing 
    } 
} 

코드에서 'ProductiveMessageHandler'또는 'TestMessageHandler'를 삽입 할 수 있으며, GoogleMock을 사용하여 조롱 된 테스트 핸들러를 사용하여 기대를 테스트 할 수도 있습니다.

class MyProductionCode 
{ 
    MyProductionCode(MessageHandler *useThisInjectedMessageHandler); 
} 

귀하의 testcode은 다음과 같습니다

class TestMyProductionCode : public ::testing::Test 
{ 
    TestMessageHandler myTestMessageHandler; 
} 

TEST(TestMyProductionCode, ExampleTest) 
{ 
    MyProductionCode myTestClass(&myTestMessageHandler); 

    ASSERT_TRUE(myTestClass.myTestFunction()); 
} 
관련 문제