본문 바로가기

Language/c/c++

CppUnit

 

CppUnit #

  • CppUnit는 C++용 테스팅 프레임워크이다. Michael Feathers씨가 Java의 JUnit을 C++로 구현한 것이다.
  • 다음과 같은 특징을 갖는다.

    • XML output with hooks for additional data (XSL format avaliable in release 1.10.2 needs some FiXing)

    • Compiler-like text output to integrate with an IDE

    • Helper macros for easier test suite declaration

    • Hierarchical test fixture support

    • Test registry to reduce recompilation need

    • Test plug-in for faster compile/test cycle (self testable dynamic library)

    • Protector to encapsulate test execution (allow capture of exception not derived from std::exception)

    • MfcTestRunner

    • QtTestRunner, a Qt based graphic test runner

    • QxTestRunner, a Qt4 based graphic test runner

    • CursesTestRunner

    • WxWidgetsTestRunner (formerly: WxWindowsTestRunner)

CppUnit 다운로드#

  • http://sourceforge.net/projects/cppunit 에서 최근 파일을 다운로드한다.
  • 적당한 곳에 압축을 푼다. 압축을 푼곳의 디렉토리를 편의상 $CPPUNIT 이라고 칭한다.

VC++ 설정 하기#

CppUnit 라이브러리 빌드하기 #

  • $CPPUNIT/src/CppUnitLibraries.dsw를 VC++을 사용하여 연다.
  • Build메뉴에서 "Batch Build..."를 선택한다.
  • 라이브러리 빌드가 끝나면 $CPPUNIT/lib 디렉토리에 저장될 것이다.

VC++ 디렉토리 설정하기#

  • Tools/Options 메뉴를 설정한다.
  • Directories 탭을 선택하여 다음과 같이 설정한다.

    • Library Files 에 $CPPUNIT/lib 를 추가한다.
    • Header Files 에 $CPPUNIT/include 를 추가한다.

간단 사용기#

시작하기#

  • 새로운 프로젝트를 만든다.(콘솔 어플리케이션으로 한다.)
  • Project setting에서 다음을 수정한다.

    • C/C++ 탭에서 Code Generation을 선택하여 Release은 Multithreaded DLL을 Debug는 Debug Multithreaded DLL을 선택한다.
    • C/C++ 탭에서 C++ Language을 선택하여 Enable Run-Time Type Information(RTTI)을 체크한다.
    • Link 탭에서 Object/Library 모듈 필드에 Release는 cppunit.lib를, Debug는 cppunitd.lib를 추가해 준다.

테스트 수행 코드 만들기#

  • 이곳에서는 post-build testing을 위해 CompilerOutputter를 사용하는TextTestRunner를 선택할 것이다.
#include "stdafx.h"
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>

int main(int argc, char* argv[])
{
  // Get the top level suite from the registry
  CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();

  // Adds the test to the list of test to run
  CppUnit::TextUi::TestRunner runner;
  runner.addTest( suite );

  // Change the default outputter to a compiler error format outputter
  runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
                                                       std::cerr ) );
  // Run the tests.
  bool wasSucessful = runner.run();

  // Return error code 1 if the one of test failed.
  return wasSucessful ? 0 : 1;
}

 

테스트 코드 추가하기#

  • 다음의 헤더파일과 소스파일을 추가한다.

MoneyTest.h

#if !defined(AFX_MONEYTEST_H__833FAB42_8C68_44A7_8B27_A70007883742__INCLUDED_)
#define AFX_MONEYTEST_H__833FAB42_8C68_44A7_8B27_A70007883742__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <cppunit/extensions/HelperMacros.h>

class MoneyTest : public CppUnit::TestFixture
{
    CPPUNIT_TEST_SUITE(MoneyTest);    ///< declares that our Fiture's test suite
    CPPUNIT_TEST(testConstructor);    ///< adds a test to our test suite. The test is implemented by a method named testConstructor()
    CPPUNIT_TEST_SUITE_END();
public:
    MoneyTest();
    virtual ~MoneyTest();

    void setUp();        ///< setUp some fixtures
    void tearDown();    ///< tearDown some fixtures

    void testConstructor();

};

#endif // !defined(AFX_MONEYTEST_H__833FAB42_8C68_44A7_8B27_A70007883742__INCLUDED_)

 

MoneyTest.cpp

#include "stdafx.h"
#include "MoneyTest.h"

/// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION(MoneyTest);

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

MoneyTest::MoneyTest()
{ }

MoneyTest::~MoneyTest()
{ }

void MoneyTest::setUp()
{ }

void MoneyTest::tearDown()
{ }

void MoneyTest::testConstructor()
{
    CPPUNIT_FAIL("not implemented");
}

 

  • 출력결과는 다음과 같다.

result1.JPG

 

레퍼런스

 

이 글은 스프링노트에서 작성되었습니다.