LoginSignup
7
7

More than 5 years have passed since last update.

ファイルを保存したときにテストを自動で実行する

Last updated at Posted at 2014-10-14

はじめに

GO言語を使ったTDDに慣れて来て、毎回、go testを手動で実行するのも面倒だなぁと思い始めた矢先。
先日の「TDDBC Sendai 4th」でRubyチームがテストを自動実行していたのを見て、自分のGO環境も同じように構築したときのメモ。

今回構築するとできあがる環境

  • ファイルを保存する度に自動でテストを実行

使用するツール

  • Sublime Text 3:テキストエディタ
  • GoSublime:Sublime Text 3 のプラグイン
  • gospel:Rspec風のテスティングフレームワーク
  • bundler:Rubyのgemパッケージ管理ツール
  • guard-go:ファイル監視ツール

前提条件

既に下記のツールや実行環境がインストール済みであることを前提とします。

  1. Sublime Text 3
  2. GoSublime
  3. gospel
  4. Ruby

自動テスト環境セットアップ

ファイルが保存される度に自動でテストを実行する環境を構築します。セットアップするツールは下記の2つです。

  1. bundler
  2. guard-go

bundler

$ gem install bundler

初期化

$ cd /your/project/dir
$ bundle init

Gemfile修正

gem 'guard-go'

guard-go

インストール

$ bundle install

Guardfile 生成

コマンドを使って、Guardfileを生成します。

$ bundle exec guard init go

設定

ファイルが保存されたら、自動でテストが実行されるように設定を変更します。
Guardfileに :test => true を追加

guard 'go', :server => 'app.go', :test => true do
  watch(%r{\.go$})
end

追加した「:test」以外に「:build_only」や、「:args」、「cmd」があります。詳しくは本家を参照してください。

Guard実行

:serverに設定したファイルを作成したあと、guardを起動します。

$ touch app.go 
$ bundle exec guard

:serverに指定したファイルがないと…

こんなエラーが発生します。

21:43:48 - ERROR - Invalid Guardfile, original error is:
> [#] Server file not found. Check your :server option in your Guarfile.

サンプルコード

GitHubにサンプルコードを用意しました。

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7