2010-07-02 4 views
0

NUnit에서, TestFixtureSetup 속성은 조명기의 테스트 전에 한 번 실행해야하는 메소드를 표시하는 데 사용할 수 있습니다.CppUnit의 TestFixtureSetUp와 같은 메소드

CppUnit과 비슷한 개념이 있습니까?

이 개념을 지원하는 C++ 단위 테스트 프레임 워크가 있습니까? 아래의 답변에 따라

, 여기 ( this 질문에 대한 답변의 조언 아래)이 수행 한 예입니다 : 당신이 고정 생성자를 사용할 수 없기 때문에

// Only one instance of this class is created. 
class ExpensiveObjectCreator 
{ 
public: 
    const ExpensiveObject& get_expensive_object() const 
    { 
     return expensive; 
    } 

private: 
    ExpensiveObject expensive; 
}; 

class TestFoo: public CppUnit::TestFixture 
{ 
    CPPUNIT_TEST_SUITE(TestFoo); 
    CPPUNIT_TEST(FooShouldDoBar); 
    CPPUNIT_TEST(FooShouldDoFoo); 
    CPPUNIT_TEST_SUITE_END(); 

public: 
    TestFoo() 
    { 
     // This is called once for each test 
    } 

    void setUp() 
    { 
     // This is called once for each test 
    } 

    void FooShouldDoBar() 
    { 
     ExpensiveObject expensive = get_expensive_object(); 
    } 

    void FooShouldDoFoo() 
    { 
     ExpensiveObject expensive = get_expensive_object(); 
    } 

private: 
    const ExpensiveObject& get_expensive_object() 
    { 
     static ExpensiveObjectCreator expensive_object_creator; 
     return expensive_object_creator.get_expensive_object(); 
    } 
}; 

답변

0

이이 CppUnit을이 인스턴스를 사용하고 있음을 의미한다 테스트 당. 난 당신이 많은 시간을 읽고 처음에만 작성된 정적 부울 플래그 및 메서드가 있어야한다고 생각합니다. 그런 다음 이것을 생성자에서 호출하십시오.

+0

Fixture 생성자는 모든 테스트마다 여전히 호출 된 것처럼 보입니다. –

+0

정적을 사용하는 것이 의미가있는 것처럼 보입니다. 나는 그것을 시도 할 것이다. 더 나은 솔루션이 있는지 궁금해. 이것은 매우 일반적인 관용구처럼 보입니다. –

+0

사실 이것은 실망하고 있습니다. 유창한 아이디어가 설정되고 테스트마다 하나씩 테스트가 중단됩니다 (http://martinfowler.com/bliki/JunitNewInstance.html 및 http : // blogs). .msdn.com/b/jamesnewkirk/archive/2004/12/04/275172.aspx –

관련 문제