09
Nov

use autotest on merb with rspec

首先要在确保Merb App的$approot$/autotest/discover.rb文件中加载了”merb”, “rspec”。
discover.rb内容如下
Autotest.add_discovery { "merb" }
Autotest.add_discovery { "rspec" }

安装必要的依赖包
$: sudo apt-get install libgtk2-ruby libnotify-dev
$: wget http://rubyforge.org/frs/download.php/27134/ruby-libnotify-0.3.3.tar.bz2
$: tar jxf ruby-libnotify-0.3.3.tar.bz2
$: cd ruby-libnotify-0.3.3/
$: ruby extconf.rb
$: make && sudo make install


检查libnotify安装是否正确
irb
irb(main):001:0> require 'rnotify'
=> true
irb(main):002:0> quit

autotest会将测试结果信息发送至rontify,我们需要创建一个名为.autotest的脚本并放置在$HOME目录下。这个脚本要正常运行需要launchy Gem和一组icon,现在我们来依次完成。
$: sudo gem install launchy
$: wget http://thelucid.com/files/autotest_images.zip
$: unzip autotest_images.zip
$: mv autotest_images ~/.autotest_images

如果需要对Rspec的测试结果以HTML格式输出就需要对App的spec/spec.opts文件进行编辑,添加如下内容
--format p
--format html:doc/spec/report.html

上面的设置使报告的结果以Html形式保存在了doc/spec目录中,需要手动创建这个目录。
mkdir -p doc/spec
最后就是~/.autotest脚本的内容:
require 'rnotify'
require 'gtk2'
require 'launchy'
module Autotest::RNotify
class Notification
attr_accessor :verbose, :image_root, :tray_icon, :notification,
:image_pass, :image_pending, :image_fail,
:image_file_pass, :image_file_pending, :image_file_fail,
:status_image_pass, :status_image_pending, :status_image_fail
def initialize(timeout = 5000,
image_root = "#{ENV['HOME']}/.autotest_images” ,
report_url = “doc/spec/report.html”,
verbose = false)
self.verbose = verbose
self.image_root = image_root
self.image_file_pass = “#{image_root}/pass.png”
self.image_file_pending = “#{image_root}/pending.png”
self.image_file_fail = “#{image_root}/fail.png”
raise(”#{image_file_pass} not found”) unless File.exists?(image_file_pass)
raise(”#{image_file_pending} not found”) unless File.exists?(image_file_pending)
raise(”#{image_file_fail} not found”) unless File.exists?(image_file_fail)
puts ‘Autotest Hook: loading Notify’ if verbose
Notify.init(’Autotest’) || raise(’Failed to initialize Notify’)
puts ‘Autotest Hook: initializing tray icon’ if verbose
self.tray_icon = Gtk::StatusIcon.new
tray_icon.pixbuf = Gdk::Pixbuf.new(image_file_pending,22,22)
tray_icon.tooltip = ‘RSpec Autotest’
puts ‘Autotest Hook: Creating Notifier’ if verbose
self.notification = Notify::Notification.new(’X', nil, nil, tray_icon)
notification.timeout = timeout
puts ‘Autotest Hook: Connecting mouse click event’ if verbose
tray_icon.signal_connect(”activate”) do
Launchy::Browser.new.visit(report_url)
end
Thread.new { Gtk.main }
sleep 1
tray_icon.embedded? || raise(’Failed to set up tray icon’)
end
def notify(icon, tray, title, message)
notification.update(title, message, nil)
notification.pixbuf_icon = icon
tray_icon.tooltip = “Last Result: #{message}”
tray_icon.pixbuf = tray
notification.show
end
def passed(title, message)
self.image_pass ||= Gdk::Pixbuf.new(image_file_pass, 48, 48)
self.status_image_pass ||= Gdk::Pixbuf.new(image_file_pass, 22, 22)
notify(image_pass, status_image_pass, title, message)
end
def pending(title, message)
self.image_pending ||= Gdk::Pixbuf.new(image_file_pending, 48, 48)
self.status_image_pending ||= Gdk::Pixbuf.new(image_file_pending, 22, 22)
notify(image_pending, status_image_pending, title, message)
end
def failed(title, message)
self.image_fail ||= Gdk::Pixbuf.new(image_file_fail, 48, 48)
self.status_image_fail ||= Gdk::Pixbuf.new(image_file_fail, 22, 22)
notify(image_fail, status_image_fail, title, message)
end
def quit
puts ‘Autotest Hook: Shutting Down…’ if verbose
#Notify.uninit
Gtk.main_quit
end
end
Autotest.add_hook :initialize do |at|
@notify = Notification.new
end
Autotest.add_hook :ran_command do |at|
results = at.results.last
unless results.nil?
output = results[/(\d+)\s+examples?,\s*(\d+)\s+failures?(,\s*(\d+)\s+pending)?/]
if output
failures = $~[2].to_i
pending = $~[4].to_i
end
if failures > 0
@notify.failed(”Tests Failed”, output)
elsif pending > 0
@notify.pending(”Tests Pending”, output)
else
unless at.tainted
@notify.passed(”All Tests Passed”, output)
else
@notify.passed(”Tests Passed”, output)
end
end
end
end
Autotest.add_hook :quit do |at|
@notify.quit
end
end

No Comments

Be the first to comment on this entry.

Leave a comment

Name(required)
Mail (will not be published)(required)
Website

Fields in bold are required. Email addresses are never published or distributed.

Some HTML code is allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
URLs must be fully qualified (eg: http://www.raecoo.com),and all tags must be properly closed.

Line breaks and paragraphs are automatically converted.

Please keep comments relevant. Off-topic, offensive or inappropriate comments may be edited or removed.

    About

    Tag Cloud