「CppUnit」の版間の差分

提供: C++入門
移動: 案内検索
(ページの作成:「<!-- vim: filetype=mediawiki --> CppUnit は、 C++ 向けのユニットテストフレームワークです。 読み方 ;CppUnit: しーぴーぴー ...」)
 
 
(同じ利用者による、間の1版が非表示)
行1: 行1:
<!--
+
[[CppUnit]] は、 [[C++]] 向けの[[ユニットテストフレームワーク]]です。
vim: filetype=mediawiki
+
-->
+
 
+
[[CppUnit]] は、 [[C++]] 向けのユニットテストフレームワークです。
+
 
+
読み方
+
  
 +
'''読み方'''
 
;[[CppUnit]]: しーぴーぴー ゆにっと
 
;[[CppUnit]]: しーぴーぴー ゆにっと
  
行14: 行9:
  
 
== インストール ==
 
== インストール ==
 
 
{{ports|/usr/ports/devel/cppunit|cppunit}}
 
{{ports|/usr/ports/devel/cppunit|cppunit}}
 
 
{{yum|cppunit cppunit-devel cppunit-doc}}
 
{{yum|cppunit cppunit-devel cppunit-doc}}
 
 
== テストフレームワークのサンプルコード ==
 
== テストフレームワークのサンプルコード ==
 
 
=== foo.hpp ===
 
=== foo.hpp ===
 
 
プログラムの実体。
 
プログラムの実体。
 
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
 
#ifndef FOO_HPP
 
#ifndef FOO_HPP
行38: 行27:
  
 
=== テストコード ===
 
=== テストコード ===
 
 
==== fooTest.hpp ====
 
==== fooTest.hpp ====
 
 
テストコードの実体。
 
テストコードの実体。
 
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
 
#include <cppunit/TestCase.h>
 
#include <cppunit/TestCase.h>
行73: 行59:
 
         }
 
         }
 
};
 
};
</syntaxhighlight>
+
</syntaxhighlight
 
+
 
==== fooMain.cpp ====
 
==== fooMain.cpp ====
 
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
 
#include "fooTest.hpp"
 
#include "fooTest.hpp"
行89: 行73:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
== コンパイル ==
 
== コンパイル ==
 
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
g++ -I. `cppunit-config --cflags`  `cppunit-config --libs` fooMain.cpp
 
g++ -I. `cppunit-config --cflags`  `cppunit-config --libs` fooMain.cpp
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
== 実行例 ==
 
== 実行例 ==
 
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
% ./a.out
 
% ./a.out
行107: 行87:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
== 関連項目 ==
 
== 関連項目 ==
 
 
* [[ユニットテストフレームワーク]]
 
* [[ユニットテストフレームワーク]]
 +
<!-- vim: filetype=mediawiki
 +
-->

2015年4月25日 (土) 14:49時点における最新版

CppUnit は、 C++ 向けのユニットテストフレームワークです。

読み方

CppUnit
しーぴーぴー ゆにっと

概要

インストール

FreeBSDにインストールする場合

ports コレクションからインストールする場合

cd /usr/ports/devel/cppunit
sudo make install clean

pkgコマンドでインストールする場合

sudo pkg install cppunit

portmasterコマンドでインストールする場合

sudo portmaster -y -d /usr/ports/devel/cppunit

CentOSにインストールする場合

CentOS に yum コマンドでインストールする場合。

sudo yum -y install cppunit cppunit-devel cppunit-doc

テストフレームワークのサンプルコード

foo.hpp

プログラムの実体。

#ifndef FOO_HPP
#define FOO_HPP
class foo {
        public:
                int add(int a, int b) {
                        return a+b;
                }
};
#endif // FOO_HPP

テストコード

fooTest.hpp

テストコードの実体。

#include <cppunit/TestCase.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/ui/text/TestRunner.h>
 
#include "foo.hpp"
 
class fooTest: public CppUnit::TestFixture {
 
        CPPUNIT_TEST_SUITE ( fooTest );
        CPPUNIT_TEST ( test_add );
        CPPUNIT_TEST_SUITE_END ();
 
        private:
 
        foo *f;
 
        public:
        void setUp () {
                f = new foo ();
        }
 
        void tearDown () {
                delete f;
        }
 
        void test_add () {
                CPPUNIT_ASSERT ( f->add(1,2) == 3 );
        }
};
</syntaxhighlight
====UNIQa63827e2ab7e8502-h-6--QINU fooMain.cpp ====
UNIQa63827e2ab7e8502-syntaxhighlight-00000006-QINU

コンパイル

g++ -I. `cppunit-config --cflags`  `cppunit-config --libs` fooMain.cpp

実行例

% ./a.out
.
 
 
OK (1 tests)

関連項目