`
lihbobo
  • 浏览: 66845 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Automatically Measuring Your Code Coverage Using Selenium (or Watir) and Rcov

 
阅读更多
desc "Measure coverage of selenium specs"
task :coverage do
  FileUtils.rm_rf "selenium_coverage"
  mongrel_pid = fork { exec("rcov script/server -o selenium_coverage --rails -- -e test -p 4000") }
  sleep(6) # wait for mongrel to start

  begin # selenium will call "exit 1" if there are failures
    Rake::Task["selenium:spec"].invoke
  rescue SystemExit; end

  Process.kill("INT", mongrel_pid) # triggers generation of rcov report
  Process.wait
  system("open selenium_coverage/index.html") if PLATFORM['darwin']
end


Custom Maintenance Pages
January 05, 2007

Update: The Advanced Rails Recipes book includes an updated and extended version of this recipe. Thanks for your support!

Capistrano makes it quick and easy to put up a temporary maintenance page while you're doing chores around your production Rails app. Hopefully users won't see that page for long, but even when they do it's a nice touch to customize the page a smidge.

The stock maintenance page template that Capistrano uses by default is good, but it's easy to set your app apart from the rest. Simply redefine the disable_web task in your deploy.rb file to render a custom template. Here's an example:

task :disable_web, :roles => :web do
  on_rollback { delete "#{shared_path}/system/maintenance.html" }
 
  maintenance = render("./app/views/layouts/maintenance.rhtml",
                       :deadline => ENV['UNTIL'],
                       :reason => ENV['REASON'])
                      
  put maintenance, "#{shared_path}/system/maintenance.html",
                   :mode => 0644
end

This task uses ERb to render your local maintenance.rhtml template, and transfers the result to the maintenance.html file on all remote hosts in the web role.

I tend to put the maintenance.rhtml template in the layouts directory because it's a full HTML file like the other layout files, not just a fragment of HTML. Here's an example maintenance.rhtml template, sans all the surrounding HTML:

<h1>
  We're currently down for <%= reason ? reason : "maintenance" %>
  as of <%= Time.now.strftime("%H:%M %Z") %>.
</h1>
<p>
  Sorry for the inconvenience. We'll be back
  <%= deadline ? "by #{deadline}" : "shortly" %>.
  Please email us if you need to get in touch.
</p>

You'll also need to tell your web server to check for the static maintenance file and redirect all requests to it if the file exists. Here's an example for Apache:

RewriteCond %{REQUEST_URI} !\.(css|jpg|png)$
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /system/maintenance.html [L]

Then, when it's time for some app maintenance, you can put up your custom maintenance page by typing

cap disable_web

Here's an example of what you'd see if we were cleaning out the dust bunnies on one of our apps:
Maintenance

And when you've finished, simply take down the maintenance page by typing

cap enable_web

Another benefit of redefining disable_web and using a custom template is being able to define in any variables you like. Here's how you'd pass in the two variables used in the template above, but you can imagine defining an arbitrary number of variables:

UNTIL="16:00 MST" REASON="a database upgrade" cap disable_web

Ideally your site would never be down, but once in a while you need to do some preventative maintenance. People who use your app will appreciate that you've taken the time to spruce up the edge cases.
rcov Rake Task
January 05, 2007

Jamis recently posted a tip on the virtues of running rcov to measure code coverage. While it's easy to put too much faith in code coverage, running rcov is an inexpensive way to get some insight into your testing patterns. I've been using it on recent Rails projects to get a quick synoptic view of the test landscape.

If you'd like to give rcov a try, here are the steps I used to get it working:

1. Install rcov

gem install rcov

2. Write a Rake Task

Add this task to your lib/tasks/my_app.rake file, for example:

namespace :test do

  desc 'Measures test coverage'
  task :coverage do
    rm_f "coverage"
    rm_f "coverage.data"
    rcov = "rcov --rails --aggregate coverage.data --text-summary -Ilib"
    system("#{rcov} --no-html test/unit/*_test.rb")
    system("#{rcov} --no-html test/functional/*_test.rb")
    system("#{rcov} --html test/integration/*_test.rb")
    system("open coverage/index.html") if PLATFORM['darwin']
  end

end

I've tried various approaches and options over the last few months, and these seem to work best for me. The results for all the unit, functional, and integration tests get aggregated into one report so I can scan everything quickly. Your mileage may vary.

3. Run the Task

rake test:coverage

You'll get a text summary for the unit, functional, and integration tests as they're being run. And if you're on a Mac, the task automatically pops open the HTML report in your default browser at the end. Otherwise, you can open it manually.

Enjoy!
分享到:
评论

相关推荐

    Selenium Design Patterns and Best Practices(PACKT,2014)

    Improve your test suite continuously by refactoring code and using the DRY principle Stabilize your tests by using patterns such as the Action Wrapper and Black Hole Proxy patterns

    Code Warehouse Personal Edition v1.0

    Code stored in Code Warehouse can be searched by keywords, authors, dates, names of code units (application, class, or procedure), and words in code or comments. Keywords are automatically generated ...

    ASP.NET MVC with Entity Framework and CSS [2016]

    • Azure deployment using Code First Migrations, remote debugging and viewing/editing your remote data • CSS Styling including animation and media queries What You Will Learn: • Get up and running...

    code-collaborator

    Whether you're a small startup or a large enterprise, integrating Code Collaborator into your development process can significantly improve the efficiency and effectiveness of your code reviews.

    vc2005入门经典code

    Code from the book is in the "57194 ... To use the source files in your own projects, you can either copy the source code to a file in your project or add the file to your project. Have fun! Ivor Horton

    SourceNavigator_6.0

    You can also build your projects, either with your own makefile, or by using Source-Navigator's build system to automatically generate a makefile. Source-Navigator works with the Insight GUI ...

    Create your own analog meters using MSPaint. These .bmp or .

    Create your own analog meters using MSPaint. These .bmp or .gif files become your meter face. Use the (analogmeter) subroutine to automatically draw the meter needle over the meter face

    Exploring Swift Playgrounds

    And the playgrounds you build and use on your Mac and on your iPad are automatically shared using your Apple ID. Exploring Swift Playgrounds shows you how to use playgrounds to try out your basic ...

    Android代码-Door Code Notifier

    No longer will you have to look for the code, or call your friend: the information will automatically come to you when and where you need it! The notification also presents handy 'Text' and 'Call' ...

    Automatically sync your leetcode solutions to your github .zip

    标题 "Automatically sync your leetcode solutions to your github .zip" 提供的信息是关于一个自动同步 LeetCode 解决方案到 GitHub 的工具或流程。这个过程通常涉及到编程自动化、版本控制和持续集成的概念,以下...

    ModelMaker.Code.Explorer.v8.0.0.1877.part1

    As a Refactoring Editor, it makes changing code easy and fast: Classes and members can be created and modified through drag and drop or by selecting options in dedicated dialogs. You to Refactor, edit...

    ModelMaker.Code.Explorer.v8.0.0.1877.part2

    As a Refactoring Editor, it makes changing code easy and fast: Classes and members can be created and modified through drag and drop or by selecting options in dedicated dialogs. You to Refactor, edit...

    Test-Driven Development with Python [2017]

    Test-Driven Development with Python: Obey the Testing Goat: Using Django, Selenium, and JavaScript by Harry J. W. Percival English | 2 Aug. 2017 | ISBN: 1491958707 | ASIN: B074HXXXLS | 604 Pages | AZW...

    Sending Email using MAPI - A COM DLL(sending email demo and soucecode)

    It can be used to send emails automatically with customized user messages and attachments to specific people at particular time, especially while using some exe servers or NT services. That's it. ...

    UE(官方下载)

    The benefit of a column maker is that it can help you to format your text/code, or in some cases to make it easier to read in complex nested logic. Quick Open UltraEdit and UEStudio provide multiple ...

    Automatically exported from code.google.compprotobuf-matlab.zip

    标题和描述中提到的"Automatically exported from code.google.compprotobuf-matlab.zip"表明这是一个从code.google.com导出的项目,与Protocol Buffers(protobuf)和MATLAB有关。protobuf是一种序列化数据的开源...

Global site tag (gtag.js) - Google Analytics