「私は自分のことを書いてください」と言いたくなります。一方、私が書いたもの、つまりtest_util.hppとtest_util.cppを再利用したいかもしれません。 1つの定義をcppファイルからhppファイルにインライン展開するのは簡単です。 MITのライセンス。私はまた、この答えにそれを貼り付けました。より多くのインスピレーションを得るため
#include "test_util.hpp"
bool test_one() {
bool ok = true;
CHECK_EQUAL(1, 1);
return ok;
}
int main() {
bool ok = true;
ok &= test_one();
// Alternatively, if you want better error reporting:
ok &= EXEC(test_one);
// ...
return ok ? 0 : 1;
}
ブラウズ周りtestsディレクトリ内:
は
これは、あなたがこのようなテストファイルを書き込むことができます。
// By Magnus Hoff, from http://stackoverflow.com/a/9964394
#ifndef TEST_UTIL_HPP
#define TEST_UTIL_HPP
#include <iostream>
// The error messages are formatted like GCC's error messages, to allow an IDE
// to pick them up as error messages.
#define REPORT(msg) \
std::cerr << __FILE__ << ':' << __LINE__ << ": error: " msg << std::endl;
#define CHECK_EQUAL(a, b) \
if ((a) != (b)) { \
REPORT(\
"Failed test: " #a " == " #b " " \
"(" << (a) << " != " << (b) << ')' \
) \
ok = false; \
}
static bool execute(bool(*f)(), const char* f_name) {
bool result = f();
if (!result) {
std::cerr << "Test failed: " << f_name << std::endl;
}
return result;
}
#define EXEC(f) execute(f, #f)
#endif // TEST_UTIL_HPP
あなたがテストを実行している場合、あなたはおそらく、デバッグモードでコンパイルしたいです。 –
'assert.h'をインクルードする前に' DEBUG'を定義しようとしましたか?うまくいくかもしれないが、確かに。 –
@OliCharlesworth、必ずしもそうではありません。 – user1306239