LoginSignup
2
0

More than 5 years have passed since last update.

rspec実行時に結果をOSXの通知センターに通知したい

Last updated at Posted at 2017-09-11

やりたかった事

  • テストが重く、意図した場所だけ実行する事が多いのでrspecをguardからの自動実行ではなく手動実行したい
  • テスト実行後、時間がかかることが多いので、終了時に結果を通知センターに通知したい
  • JenkinsサーバはLinuxなので自端末のみに影響をとどめたい

既に似たような事をやっているケース

実際にやったこと

~/.rspec_files/terminal_notifier_formatter.rb にフォーマット用のファイルを用意して、
~/.rspec でrequire とformatを指定することで独自に用意したフォーマットを指定しました。

やってること自体は、ほぼ、ここのままなのですが、既存の出力もする様にするため、Progressを継承してみました。

~/.rspec_files/terminal_notifier_formatter.rb
require 'rspec/core/formatters/base_formatter'
require 'terminal-notifier'

class TerminalNotifierFormatter < RSpec::Core::Formatters::ProgressFormatter
  SUCCESS_EMOJI = "\u2705"
  FAILURE_EMOJI = "\u26D4"

  RSpec::Core::Formatters.register self, :dump_summary

  def dump_summary(notification)
    body = "Finished in #{notification.formatted_duration}\n#{notification.totals_line}"
    title = if notification.failure_count > 0
      "#{FAILURE_EMOJI} #{directory_name}: #{notification.failure_count} failed example#{notification.failure_count == 1 ? nil : 's'}"
    else
      "#{SUCCESS_EMOJI} #{directory_name}: Success"
    end
    notify title, body
  end

  private

  def directory_name
    File.basename File.expand_path '.'
  end

  def notify(title, body)
    TerminalNotifier.notify body, title: title
  end
end
~/.rspec
--require ~/.rspec_files/terminal_notifier_formatter
--format TerminalNotifierFormatter

結果

rspecを実行して5分後などの忘れたころに通知が来る様になり、テスト終了タイミングを知るためにコンソールを開き続けて置く必要がなくなりました。
また、プロジェクトソースを変更する必要がないので、そのまま横に流用できます。

疑問点

  • ~/.rspec_files ってどうなん……?(そもそも.rspecで別のファイルを置くこと自体が想定されてないぽいですが)
2
0
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
2
0