~cpp ...cppunitexamplesexamples.dsw 을 연뒤 ~cpp WorkSpace 의 ~cpp FileView 에서 ~cpp CppUnitTestApp files 에 Set as Active Project로 맞춰준다.(기본값으로 되어 있다.) 그리고 컴파일 해주면 lib 폴더에 library 화일들이 생성될 것이다.
~cpp ...cppunit-x.x.xlib ~cpp ...\cppunit-x.x.xinclude
~cpp copy c:cppunitlibtestrunnerd.dll .
~cpp
#include <msvc6/testrunner/testrunner.h>
#include <cppunit/extensions/testfactoryregistry.h>
.
.
.
BOOL CMyApp::InitInstance () {
.
.
// Dialog Based 의 경우는 dlg.DoModal ()을 실행하기 전에 적어준다.
TestRunner runner;
runner.addTest ( CppUnit::TestFactoryRegistry::getRegistry().makeTest() );
runner.run ();
}
~cpp
#ifndef CPP_UNIT_EXAMPLETESTCASE_H
#define CPP_UNIT_EXAMPLETESTCASE_H
#include <cppunit/TestCase.h>
#include <cppunit/extensions/HelperMacros.h>
class ExampleTestCase : public CppUnit::TestCase
{
CPPUNIT_TEST_SUITE( ExampleTestCase ); // TestSuite
CPPUNIT_TEST( testExample ); // TestCase 의 등록.
CPPUNIT_TEST_SUITE_END(); // TestSuite 의 끝. 이로서 해당 Test Case 에 자동으로 suite 메소드가 만들어진다.
protected:
void testExample ();
public:
void setUp ();
void tearDown ();
};
#endif
~cpp
#include "stdafx.h" // MFC 인 경우.
#include "hostapp.h" // MFC 인 경우 해당 App Class
#include "ExampleTestCase.h"
CPPUNIT_TEST_SUITE_REGISTRATION( ExampleTestCase ); // TestSuite 를 등록하기. TestRunner::addTest 가 필요없다.
void ExampleTestCase::testExample () // 테스트 하려는 함수.
{
CPPUNIT_ASSERT (1 == 2);
}
void ExampleTestCase::setUp () // fixture 관련 셋팅 코드.
{
}
void ExampleTestCase::tearDown () // fixture 관련 셋팅 코드.
{
}
~cpp
#include <iostream>
using namespace std;
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/TextTestResult.h>
#ifndef _SIMPLE_H_
#define _SIMPLE_H_
#include <cppunit/TestCase.h>
#include <cppunit/extensions/HelperMacros.h>
class SimpleTest : public CppUnit::TestFixture {
public:
CPPUNIT_TEST_SUITE( SimpleTest );
CPPUNIT_TEST ( testOne );
CPPUNIT_TEST ( testTwo );
CPPUNIT_TEST_SUITE_END();
void testOne () {
CPPUNIT_ASSERT( 1 == 2 );
CPPUNIT_ASSERT( 3 == 4 );
}
void testTwo () {
CPPUNIT_ASSERT( 1 == 1 );
CPPUNIT_ASSERT( 3 == 3 );
}
};
#endif
int main( int argc, char* argv[] )
{
CppUnit::TextUi::TestRunner runner;
CppUnit::TextTestResult result;
runner.addTest (SimpleTest::suite());
runner.run("", false);
return 0;
}
~cpp #include <cppunit/ui/text/TestRunner.h>를
~cpp #include <cppunit/ui/mfc/TestRunner.h>로 수정한뒤, testrunnerd.dll 를 해당 프로젝트화일에 복사해주면 된다.
~cpp
CppUnit::MfcUi::TestRunner runner;
CppUnit::TextTestResult result;
runner.addTest (SimpleTest::suite());
// runner.run("", false);
runner.run();
~cpp
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE, LPSTR, INT) {
CppUnit::TextUi::TestRunner runner;
CppUnit::OStringStream stream;
CppUnit::TextOutputter* outputter = new CppUnit::TextOutputter(&runner.result(), stream);
runner.setOutputter(outputter);
runner.addTest(SimpleTest::suite());
runner.run();
MessageBox(NULL, stream.str().c_str(), "Test", MB_OK);
return 0;
}
~cpp
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
class SimpleTestCase : public CppUnit::TestCase {
public:
CPPUNIT_TEST_SUITE ( SimpleTestCase );
CPPUNIT_TEST(testOne);
CPPUNIT_TEST(testTwo);
CPPUNIT_TEST_SUITE_END();
public:
void testOne() {
CPPUNIT_ASSERT_EQUAL(10,10);
}
void testTwo() {
CPPUNIT_ASSERT_EQUAL(20,20);
}
};
CPPUNIT_TEST_SUITE_REGISTRATION (SimpleTestCase);
int main() {
CppUnit::TextUi::TestRunner runner;
CppUnit::TestFactoryRegistry ®istry = CppUnit::TestFactoryRegistry::getRegistry();
runner.addTest(registry.makeTest());
runner.run();
return 0;
}
~cpp #define CPPUNIT_ENABLE_NAKED_ASSERT 1
~cpp 2) Questions related to Microsoft Visual VC++ 2.1) Why does my application crash when I use CppUnit? You probably forgot to enable RTTI in your project/configuration. RTTI are disabled by default. Enable RTTI in Projects/Settings.../C++/C++ Language. Make sure to do so for all configurations. 2.2) Why does the compiler report an error when linking with CppUnit library? You most likely are not using the same C-RunTime library as CppUnit. In Release configuration, CppUnit use "Mulithreaded DLL". In Debug configurations, CppUnit use "Debug Multihreaded DLL". Check that Projects/Settings.../C++/Code Generation is indeed using the correct library.
| |
| VC++ 6.0 파일을 받으면 |
| |
| 이렇게 변환되어 컴파일 가능하다. 두개의 프로젝트가 에러남, 하지만 dll, lib 생성에는 지장없음 |
| |
| 다음과 같은 파일들을 Test Case를 작성할 프로그램에 필요하다. |
| |
| 다음과 같은 디렉토리 상태라고 가정한다. |
| |
| Include 시켜야할 디렉토리 추가(cppuint src 들어 있음) |
| |
| 라이브러리 디렉토리 세팅 |
| |
| 라이브러리 추가 |
| |
| 런타임 환경 세팅 |
~cpp Project Setting - Code Generation - Use Run-Time library 를 다음으로 맞춰준다. Release Mode : Mulithreaded DLL Debug Mode : Debug Multihreaded DLL
~cpp copy c:cppunitlibtestrunnerd.dll .
~cpp copy c:cppunitlibtestrunnerd.dll .|}}