Skip to content

Instantly share code, notes, and snippets.

@mi-yo
Created March 1, 2015 23:29
Show Gist options
  • Save mi-yo/ef06af89fbd25a3b7aa9 to your computer and use it in GitHub Desktop.
Save mi-yo/ef06af89fbd25a3b7aa9 to your computer and use it in GitHub Desktop.
CppUTest_sample

C++開発環境

msysGit

Git for Windows https://msysgit.github.io/

Git fo Windowsがあればgitは使えるが、その開発環境一式であるmsysGitを入れれば gcc等も使えるようになる。

インストール中にmsgfmtでエラーが出たら、「export LANG=C」を実行後、再度、「make install」を行う。

Eclipse

環境変数を設定 MINGW_HOME: C:\msysgit\mingw

色々参考 http://futurismo.biz/eclipsecdt4cpp

ワークスペースは C:/develop/cppsample/ws (=${workspace_loc}) とする。

CDT

Luna - http://download.eclipse.org/releases/luna

Programming Languages > C/C++ Development Tools

プロジェクト作成

New > Project... > C/C++ > C++ Project > Executables > Hello World C++ Project プロジェクト名は cppsample, toolchain は MinGW GCC を選択

  • cppsample右クリック > Build Configurations > Build All でビルド
  • Run As > Local C/C++ Application で実行

CppUTest

http://cpputest.github.io/

Download Release 3.6 as zip を ${workspace_loc}/cppsample/lib/cpputest にダウンロード

${workspace_loc}/cppsample/lib/cpputest/README_InstallCppUTest.txt を参考に、MinGWのコマンドラインで ${workspace_loc}/cppsample/lib/cpputest 下で以下を実行

configure
make
make check

TODO: include, lib設定、ビルド方法、テスト実行方法を記述

以下でお試し。

${workspace_loc}/cppsample/src/Echo.h

#ifndef ECHO_H_
#define ECHO_H_

#include <string>

class Echo {
public:
	Echo();
	virtual ~Echo();

	virtual std::string helloWorld();
	virtual std::string echo(std::string arg);
};

#endif /* ECHO_H_ */

${workspace_loc}/cppsample/src/Echo.cpp

#include "Echo.h"

Echo::Echo() {
}

Echo::~Echo() {
}

std::string Echo::helloWorld() {
	return "Hello, World!";
}

std::string Echo::echo(std::string arg) {
	return arg;
}

${workspace_loc}/cppsample/test/EchoTest.cpp

#include "CppUTest/CommandLineTestRunner.h"
#include "../src/Echo.h"

TEST_GROUP(EchoTestGroup) {

	Echo* s;

	TEST_SETUP() {
		s = new Echo();
	}

	TEST_TEARDOWN() {
		delete s;
	}
};

TEST(EchoTestGroup, HelloWorldTestSuccess) {
	LONGS_EQUAL(13, s->helloWorld().length());
	STRCMP_EQUAL("Hello, World!", s->helloWorld().c_str());
}

TEST(EchoTestGroup, HelloWorldTestFail) {
	STRCMP_EQUAL("Hellp, World!", s->helloWorld().c_str());
}

TEST(EchoTestGroup, EchoTestSuccess) {
	STRCMP_EQUAL("Echo!", s->echo("Echo!").c_str());
}

int main(int argc, char** argv) {
	return RUN_ALL_TESTS(argc, argv);
}

TODO: CppUMock お試し

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment