CppUnitでC++の例外をテストする方法
スポンサーリンク
CppUnitはC++用のテスティングフレームワーククラスライブラリで、ユニットテストを行うことができます。
C++やCの単体テストを自動化し、簡単に何度もテストすることができます。
FreeBSDはportsからcppunitをインストールできます。
test.h
テスト用コード
main.cc
テストコード本体
testKcrypt.cc
コンパイル方法
問題があるときの実行例
throwすべきときにしないときのエラーの例。
C++やCの単体テストを自動化し、簡単に何度もテストすることができます。
FreeBSDはportsからcppunitをインストールできます。
cd /usr/ports/devel/cppunit/
sudo make install clean
kcrypt.h
テストしたいソースコード。
#ifndef KCRYPT_H
# define KCRYPT_H
#include <exception>
#include <stdexcept>
#include <string>
using namespace std;
class kcrypt
{
private:
void isEmpty (const string str, const string name) {
if (str.empty () ) {
throw invalid_argument (name + " is empty.");
}
}
public:
kcrypt () {}
~kcrypt () {}
void encrypt (const string secret, const string data, string &compute) {
this->isEmpty (secret, "secret");
this->isEmpty (data, "data");
compute = "fake";
}
};
#endif /* KCRYPT_H */
test.h
テスト用コード
#ifndef TEST_H
# define TEST_H
#include <cppunit/TestCase.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/ui/text/TestRunner.h>
#include "kcrypt.h"
class kcryptTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE ( kcryptTest );
CPPUNIT_TEST ( test_encrypt_data );
CPPUNIT_TEST ( test_encrypt_secret );
CPPUNIT_TEST_SUITE_END ();
public:
void test_encrypt_secret ();
void test_encrypt_data ();
void setUp () {
target = new kcrypt;
}
void tearDown () {
delete target;
}
void runTest () {
}
private:
kcrypt *target;
};
#endif /* TEST_H */
main.cc
テストコード本体
#include <iostream>
#include <cstdlib>
#include <cppunit/ui/text/TestRunner.h>
#include "test.h"
int
main (int argc, char *argv[])
{
CppUnit::TestSuite *suite = kcryptTest::suite ();
CppUnit::TextUi::TestRunner runner;
runner.addTest (suite);
bool retcode = runner.run ();
return (! retcode);
}
testKcrypt.cc
#include <iostream>
#include <cstdlib>
#include "test.h"
using namespace std;
void
kcryptTest::test_encrypt_secret ()
{
try {
string compute;
target->encrypt ("", "data", compute);
CPPUNIT_FAIL ("The exception std::invalid_argument must be throw." );
} catch (invalid_argument &e) {
CPPUNIT_ASSERT (true);
}
}
void
kcryptTest::test_encrypt_data ()
{
try {
string compute;
target->encrypt ("secret", "", compute);
CPPUNIT_FAIL ("The exception std::invalid_argument must be throw." );
} catch (invalid_argument &e) {
CPPUNIT_ASSERT (true);
}
}
コンパイル方法
g++ -I/usr/local/include -L/usr/local/lib -lcppunit main.cc testKcrypt.cc
でもいいけど、 cppunikt-configを使った方がいいかも。
g++ `cppunit-config --cflags` `cppunit-config --libs` main.cc testKcrypt.cc
問題ないときの実行例
% ./a.out .. OK (2 tests)
問題があるときの実行例
throwすべきときにしないときのエラーの例。
..F !!!FAILURES!!! Test Results: Run: 2 Failures: 1 Errors: 0 1) test: kcryptTest::test_encrypt_secret (F) line: 14 testKcrypt.cc forced failure - The exception std::invalid_argument must be throw.
参照しているページ (サイト内): [2007-04-13-1] [2007-04-12-2]
スポンサーリンク
スポンサーリンク
いつもシェア、ありがとうございます!
もっと情報を探しませんか?
関連記事
最近の記事
- パナソニック ジェットウォッシャードルツ EW-DJ61-Wのホースの修理
- LinuxセキュリティモジュールIntegrity Policy Enforcement
- アマゾンのEcho Show 5を買ったのでレビューします
- アマゾンのサイバーマンデーはAlexa Echo Show 5が安い
- Android スマートフォン OnePlus 7T と OnePlus 7の違い
- Android スマートフォン OnePlus 7 をAndroid10にアップデートしてみた
- クレジットカードのバーチャルカードの比較のまとめ
- 活動量計 Xiaomi Mi Band 4を買ってみたのでレビュー
- Android スマートフォン OnePlus 7 のレビュー
- AliExpressでスマートフォンを買い物してみた
- パソコンのホコリ対策 レンジフードフィルターと養生テープ
- 80PLUS GOLDのPC電源ユニットAntec NeoEco 750 Goldのレビュー
- イギリスの付加価値税 VAT は払い戻しを受けられる
- イギリスのロンドンでスーツケースなど荷物を預けられる場所は
- イギリスのロンドンで地下鉄やバスに乗るならオイスターカードを使おう
- イギリスのヒースロー空港からロンドン市内への行き方
- 航空便でほかの航空会社に乗り継ぎがある場合のオンラインチェックイン
- SFC会員がANA便ではなくベトナム航空のコードシェアを試して解ったこと
- ベトナムの入国審査でeチケットの掲示が必要だった話
- シアトルの交通ICカードはオルカカード(Orca)です
人気のページ
- Windows7 IME 辞書ツールで単語の登録に失敗しました
- C言語 popen()でコマンドを実行して出力を読み込む
- Windows7で休止状態にする方法
- CentOS MySQLの起動、停止、再起動
- loggerコマンドでsyslogにエラーを出力する方法
- パソコンパーツの買取をしてくれる店のまとめ
- Java Mapの使い方 get(),put(),remove(),size(),clear()
- 楽天のRポイントカードを作ってみた
- iPhone 5 から iPhone 6 に乗り換えたのでレビュー
- netstatコマンドのステータスの意味
スポンサーリンク
過去ログ
2020 : 01 02 03 04 05 06 07 08 09 10 11 122019 : 01 02 03 04 05 06 07 08 09 10 11 12
2018 : 01 02 03 04 05 06 07 08 09 10 11 12
2017 : 01 02 03 04 05 06 07 08 09 10 11 12
2016 : 01 02 03 04 05 06 07 08 09 10 11 12
2015 : 01 02 03 04 05 06 07 08 09 10 11 12
2014 : 01 02 03 04 05 06 07 08 09 10 11 12
2013 : 01 02 03 04 05 06 07 08 09 10 11 12
2012 : 01 02 03 04 05 06 07 08 09 10 11 12
2011 : 01 02 03 04 05 06 07 08 09 10 11 12
2010 : 01 02 03 04 05 06 07 08 09 10 11 12
2009 : 01 02 03 04 05 06 07 08 09 10 11 12
2008 : 01 02 03 04 05 06 07 08 09 10 11 12
2007 : 01 02 03 04 05 06 07 08 09 10 11 12
2006 : 01 02 03 04 05 06 07 08 09 10 11 12
2005 : 01 02 03 04 05 06 07 08 09 10 11 12
2004 : 01 02 03 04 05 06 07 08 09 10 11 12
2003 : 01 02 03 04 05 06 07 08 09 10 11 12