C++ μμ UnitTestλ₯Ό νκΈ° μν UnitTestFramework. http://sourceforge.net/projects/cppunit/ μμ λ€μ΄ λ°μ μ μλ€.
C++ μ λλ€λ₯Έ ννμ UnitTestFramework λ‘ CxxTest κ° μλ€.
VC 7.0 μ© μΈν
νλ©΄ λ€λ§ μΆκ°ν©λλ€. --NeoCoin
μ΄κ³³μ VS2005μ© μ€μ λ°©λ²μ κ°λ¨νκ² μ 리ν΄λ . - μμΈν
----
C++ μ λλ€λ₯Έ ννμ UnitTestFramework λ‘ CxxTest κ° μλ€.
----
1. μ€μΉ ¶
Visual C++ 6.0 κΈ°μ€μΌλ‘ μ€λͺ
νλ€. λ€λ₯Έ μΈμ΄λ€μ λΉν΄ νκ²½μ€μ μ μν΄ ν΄μΌ ν λΆλΆμ΄ λ§μΌλ―λ‘ μΈλ΄μ¬μ κ°μ§κ³ λ°λΌν΄μΌ νλ€.
1.1.1. Library νμΌ μμ±νκΈ° ¶
μ²μ μμΆνμΌμ λ°κ³ νλ©΄ lib ν΄λμ Library κ΄λ ¨ νμΌλ€μ΄ μλ€. μ΄κ²μ λ§λλ λ°©λ²
μ΄λ‘μ κ°μ₯ κΈ°λ³Έμ΄ λλ μ ν μ μλ£λλ€.
- Library νμΌ μμ± :
~cpp ...cppunitexamplesexamples.dsw
μ μ°λ€~cpp WorkSpace
μ~cpp FileView
μμ~cpp CppUnitTestApp files
μ Set as Active Projectλ‘ λ§μΆ°μ€λ€.(κΈ°λ³Έκ°μΌλ‘ λμ΄ μλ€.) κ·Έλ¦¬κ³ μ»΄νμΌ ν΄μ£Όλ©΄ lib ν΄λμ library νμΌλ€μ΄ μμ±λ κ²μ΄λ€.
μ΄λ‘μ κ°μ₯ κΈ°λ³Έμ΄ λλ μ ν μ μλ£λλ€.
1.2.1. include, library directory λ§μΆ°μ£ΌκΈ° (λμ€ νλλ₯Ό μ ννλ€.) ¶
Library :
Include :
~cpp ...cppunit-x.x.xlib
Include :
~cpp ...\cppunit-x.x.xinclude
- μ 체 μΈν
λ³κ²½μ
- Tools -> Options -> Directories -> Include files μμ ν΄λΉ cppunit
- Tools -> Options -> Directories -> Library files μμ μμ lib
- Tools -> Options -> Directories -> Include files μμ ν΄λΉ cppunit
- ν΄λΉ νλ‘μ νΈμλ§ μ μ©μ
- Project -> Settings -> C/C++ -> Preprocessor -> Additional include directories
- Project -> Settings -> Link -> Input -> Additional Library directories
- Project -> Settings -> C/C++ -> Preprocessor -> Additional include directories
- RTTI λ₯Ό 체ν¬ν΄μ€λ€.
- Project Setting - Link - General - object/library μ cppunitd.lib, testrunnerd.lib λ₯Ό μΆκ°ν΄μ€λ€.
- ν΄λΉ νλ‘μ νΈκ° μλ κ³³μ debug λ±μ λλ ν 리μ ν΄λΉ lib λλ ν 리μ μλ testrunnerd.dll μ 볡μ¬ν΄μ€λ€. μ΄λ Project Setting - Post-Build-step μ Post-build-command μ λ€μμ μΆκ°ν΄μ£Όλ©΄ μ»΄νμΌ ν λλ§λ€ dll νμΌμ μλμΌλ‘ 볡μ¬ν΄μ€λ€.
~cpp copy c:cppunitlibtestrunnerd.dll .
- Project Setting - Code Generation - Use Run-Time library λ₯Ό λ€μμΌλ‘ λ§μΆ°μ€λ€.
- Release Mode : Mulithreaded DLL
- Debug Mode : Debug Multihreaded DLL
- Release Mode : Mulithreaded DLL
- app ν΄λμ€μ λ€μκ³Ό κ°μ runner μ€νμ μν μ½λλ₯Ό μΆκ°νλ€. λ€μ μ½λλ₯Ό μΆκ°ν¨μΌλ‘μ¨ νλ‘κ·Έλ¨ μ€νμμ GUI Test Runner κ° μ€νλλ€.
~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 (); }
2. UnitTest TestCaseμ μμ± ¶
Test Case κ° λ Classλ
~cpp CppUnit::TestCase
λ₯Ό μμλ°λλ€.
2.1. ExampleTestCase.h ¶
~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
2.2. ExampleTestCase.cpp ¶
~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; }
2.2.1. GUI Test Runner μ μ΄μ© ¶
GUI Programming μ νκΈ° μν΄ winmain μ΄ μμμΈ μ½λμ κ²½μ°(MFC GUI Programming ν¬ν¨) μ½μμ°½μ΄ λ¨μ§ μλλ€. μ΄ κ²½μ° GUI Runner λ₯Ό μ€νν΄μ€μΌ νλ€.
Runner μ€νμ½λλ λ€μκ³Ό κ°μ΄ MFC UI μ Test Runner λ₯Ό μ΄μ©νλ€.
~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();
2.2.2. Win32 API or Console νλ‘κ·Έλλ°μ ¶
Win API Programming μμ Text Runner λ₯Ό μ΄μ©νμ¬ μ΄μ© κ°λ₯. λ€μκ³Ό κ°μ μμΌλ‘ μΈ μλ μλ€.
~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; }
2.2.3. assertEqual μ μ΄μ© ¶
μ½λλ₯Ό 보면 μκ² μ§λ§, ASSERT λ¬Έλ€μ λν΄μ μ λΆ λ§€ν¬λ‘λ₯Ό μ΄μ©νλ€. λ§μΌ μ΄λ₯Ό λ€λ₯Έ μΈμ΄λ€μ UnitTest Framework μ²λΌ assertEqual μ΄λ assert λ¬ΈμΌλ‘ μ°κ³ μΆλ€λ©΄, λ€μμ λ¬Έμ₯μ cppunit library λ₯Ό include νκΈ°μ μ μΆκ°ν΄μ€λ€.
κΈ°ν μμΈν λ΄μ©λ€μ (flag λ€) cppunit/portability.h λ₯Ό μ°Έμ‘°νλ©΄ λλ€.
~cpp #define CPPUNIT_ENABLE_NAKED_ASSERT 1
2.2.4. μ£Όμμ ¶
- μ΄κΈ° μ€λΉν λ μ½μ§νλ κ²½μ°κ° λ§λ€. -_-; CppUnit μ κ²½μ°λ ν€λνμΌλ€μ include μμλ€μ΄ μ€μνλ€. κ·Έλ¦¬κ³ MFC μ κ²½μ°λ stdafx.h λ₯Ό κ°κ°μ νμΌλ€λ§λ€ include ν΄μ€μΌ νλ€. (APIμμ CppUnit λ μ΄λ¨μ§ κΆκΈν΄μ§λ€.)
- μ€κ°μ€κ°μ Rebuild ν΄μ€ νμκ° μλ€. (μ λλ‘ νλ€κ³ μκ°νλλ° Test Caseκ° Failure κ° λλ κ²½μ°μλ νλ² μμ¬ν νμκ° μλ€.)
MSVCμμ μλ¬κ° λλ κ²½μ° λλΆλΆμ CppUnit FAQλ₯Ό μ°Έκ³ νλ©΄ ν΄κ²°ν μ μλ€. http://cppunit.sourceforge.net/FAQ
~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.
1.9.10 λ²μ μ κΈ°μ€μΌλ‘ VC++ 7.0 (VS.NET) νκ²½ μΈν μ 보μΈλ€.
2.2.5. μ€λΉ - 1 lib, dll λ§λ€κΈ° ¶
1.9.10 μ sf.net μ λ¦΄λ¦¬μ¦ λμ΄ μμ§ μλ€.
http://cppunit.sourceforge.net/snapshot/ μμ λ°μμ¬μ μλ€.
http://cppunit.sourceforge.net/snapshot/ μμ λ°μμ¬μ μλ€.
2.2.6. μ€λΉ - 2 TestCase λ§λ€κΈ°λ₯Ό μν μΈν ¶
μ±κ°ν΄ κ΅λ¦½λνμ μννΈμ¨μ΄ 곡ν νλ‘μ νΈ μμ μμ CppUnitμ μ¬μ©νκ³ μλλ°, http://www.comp.nus.edu.sg/~cs3214s/tools/cppunitVC.html μ κ°λ©΄ MSVCμμ CppUnitμ μ¬μ©νλ λ°©λ²μ μ½κ² μ€λͺ ν μλ΄μλ₯Ό λ³Ό μ μλ€.
νκ΅ μμ
μμ CppUnit κ³Ό CVSλ₯Ό μ€μ λ‘ μ΄λ€λκ² μ κΈ°νλ€λ; --1002
νκ΅ μμ
μμ μ€μ§μ μ΄κ³ νμ₯μμ μ§μ μ°μ΄λ λꡬλ€μ μ¬μ©νλλ‘ μ λνλ κ²μ΄ μ λ§ λΆλ½κ³ , κ΅λ΄ νλ‘μ νΈ/μ€μ΅ μμ
μμ κ·Έλ₯ κ΅μ¬μ μ±ν° νλμ© λ°νμν€κ³ μ΄λ₯Ό μ§μΌλ³΄κ³ , νκ°νκ³ , λμ μ§μ½μ μΈ μ§λ¬ΈμΌλ‘ λ°νμ 골ν λ¨Ήμ΄λ μΌ μΈμλ, κ΅μ(νΉμ μ‘°κ΅)κ° ν΄ μ€ μ μλ κ²μ΄ μ΄λ κ² λ§λ€λ κ²μ΄ μ κΈ°νλ€λ; --JuNe
- vc7 μ© cppunit μ μλμ.? νκ²½μ΄ λ무 λ§μ΄ λ°λμ΄μ μ κ±Έ μ μ©νλ €λκΉ νλλ€μ..-_- (μΈμ€ν¨ λνλ¨ΌνΈ λ³΄λκΉ μ΅μ 6.0μ΄λΌκ³ λμμκΈ°λ νλλ°..) μμΈν
vc7 λ λκ°μ΄ νλ©΄ λ¨. λ¨, νλ‘μ νΈμ λΌμ΄λΈλ¬λ¦¬ μ€μ μ΄ vc7 μ λ€λ₯Έ λ©λ΄μ μμ΄μ κ·Έλ μ§. --1002
- μ "Project Setting - Link - General - object/library μ cppunitd.lib, testrunnerd.lib λ₯Ό μΆκ°ν΄μ€λ€."λΌκ³ ν΄μ μΆκ° ν΄μ€¬λλ
LINK : fatal error LNK1104: cannot open file "cppunitd.lib,"
Error executing link.ex
- VC6μμ μμ
νκ³ μλλ°μ. CFileDialogλ₯Ό ν΅ν΄ νμΌ pathλ₯Ό λ°μΌλ €κ³ νλλ°, TestRunnerκ° CFileDialog λͺ
λ Ήμ μννλ κ²λ³΄λ€ λ¨Όμ λμν΄ νμΌ κ²½λ‘λ₯Ό λ°μ μ μμ΅λλ€.. TestRunnerκ° μ€νλλ μμ μ μ‘°μ ν μ μλμ? --FredFrith
mainμμλ ExampleTestCaseλ₯Ό μ¬μ©νμ§ μλλ°μ...
~cpp Project Setting - Code Generation - Use Run-Time library λ₯Ό λ€μμΌλ‘ λ§μΆ°μ€λ€. Release Mode : Mulithreaded DLL Debug Mode : Debug Multihreaded DLL
- C/C++ νμ 보면 categoryκ° μλλ° κ±°κΈ°μ code generation μ΄ μμ΅λλ€. - μμΈν
~cpp copy c:cppunitlibtestrunnerd.dll .
{{|
Project Setting - Link - General - object/library μ cppunitd.lib, testrunnerd.lib λ₯Ό μΆκ°ν΄μ€λ€.
~cpp copy c:cppunitlibtestrunnerd.dll .|}}
----
kldp.net μ c λ₯Ό μν UnitTest Framework νλ‘μ νΈκ° μμλ€μ. http://kldp.net/projects/act/
----
UnitTest