在 Rails 中对暂存/生产环境进行冒烟测试的最佳方法是什么?

发布于 2024-10-31 21:01:14 字数 976 浏览 1 评论 0原文

首先,设置...

我目前正在 Mac OS X 上使用 Ruby 1.8.7 MRI 开发 Rails 3 应用程序,针对 MySQL 数据库运行测试和本地开发。我有 3 个“其他”非本地环境,我们在公司中为每个应用程序使用这些环境,称为 dev、tqa 和 prod。它们使用 JRuby (1.8.7) 在 Tomcat 中运行,并以 Oracle 作为后端。

正如您所看到的,环境有很大不同,我们在部署到本地不存在的 Oracle/JRuby 环境时遇到了一些错误(例如 Oracle 中的日期处理和指定默认模式)。

我喜欢在本地运行 Cucumber/Webrat/Capybara 之类的东西来访问应用程序中公开的每个 URL,以确保基本功能正常工作(即冒烟测试)。理想情况下,它会命中每个 url,并且会执行一些简单的操作,例如在表单中输入数据和单击按钮等。

理想情况下,当我部署到 dev/tqa 时,我会运行类似的操作,除了指向已部署的应用程序而不是本地应用程序。 Cucumber 似乎针对本地运行的应用程序进行了优化,并与 Rails 很好地集成,但无法针对所有意图的“外部”应用程序运行(或者至少我找不到实际有效的简单方法)。

另外,当我部署到产品时,我希望运行一套类似的冒烟测试,但它不会改变当前生产数据库的状态(即,只会获取 URL)。

我想可以使用像 Selenium 这样的东西,但我真的很想运行一个 rake 任务并返回结果,就像我对 Cucumber 所做的那样。

有没有任何 Rails/Ruby 方法可以做到这一点,或者其他人只是使用 wget 或 Selenium 推出自己的解决方案?

此处提出了类似的问题:自动对应用程序中的所有网页进行烟雾测试,部署后

不过,我不确定这个问题是否正是我所想的。

First, the setup...

I am currently developing a Rails 3 application on Mac OS X using Ruby 1.8.7 MRI, running tests and local dev against a MySQL database. I have 3 "other" non-local environments that we use at my company for every application called dev, tqa, and prod. These run in Tomcat using JRuby (1.8.7) with Oracle as the backend.

As you can see, the environments are quite different, and we've run into some bugs on deployment to an Oracle/JRuby environment that don't exist locally (like date handling and specifying default schemas in Oracle).

I love running something like Cucumber/Webrat/Capybara locally to hit every URL exposed in the app to make sure the basic stuff is working (ie; a smoke test). Ideally, it hits every url, and would do some simple things like entering data in forms and clicking on buttons, etc.

Ideally, when I deploy to dev/tqa, I would run something similar, except pointed at the deployed app instead of the local application. Cucumber seems optimized to hit a locally running application and integrates well with Rails, but cannot run against what is for all intents an "external" application (or at least I can't find an easy way that actually works).

Also, when I deploy to prod I'd like a similar suite of smoke tests to run, except it would not change the state of the current production database (ie, would just GET URLs).

Something like Selenium could be used, I guess, but I'd really like to just run a rake task and get back the results like I do with Cucumber.

Is there any Rails/Ruby way to go about doing this, or does everyone else just roll their own solution using wget or Selenium?

A similar question was asked here: Automatically smoke test all webpages in application, after deployment

I'm not sure that the question is exactly what I have in mind, though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

书信已泛黄 2024-11-07 21:01:14

是的,您可以使用 Cucumber 和 Capybara 编写冒烟测试,并针对远程服务器运行它们。我已经做到了并且有效。我还在一些项目上做过 curl/wget 等,但是 Cucumber+Capybara 允许您与页面交互(甚至是使用 Javascript 的页面),而不仅仅是抓取他们。

  • Capybara的Rack::Test驱动不支持远程请求;它的 Javascript 驱动程序可以。无论您是否确实需要 Javascript 在您正在测试的页面上工作,您都需要使用 Javascript 驱动程序。根据驱动程序文档将 Capybara 配置为使用 Javascript 驱动程序,并标记您的测试 @javascript。 (我推荐poltergeist/PhantomJS驱动程序;它比Selenium更快,比capybara-webkit提供更好的错误,并且易于设置。)额外的好处:你可以在测试中做需要Javascript的事情,然后你会很高兴-测试整个堆栈,包括 Javascript。
  • 编写测试,以便它们不需要自行清理,或者以在生产数据库中安全的方式进行清理。他们无法使用DatabaseCleaner。 (为了防止意外,请将测试放在自己的项目中,并使用不包含 DatabaseCleaner 的 Gemfile。)由于冒烟测试将针对远程服务器运行,因此也不能使用事务来清理,因此它们必须要么不修改数据库或必须专门删除它们创建的对象。
  • 设置 Capybara.app_host = "http://your-server.yourco.com"
  • 设置 Capybara.run_server = false (不是必需的,但运行本地服务器没有意义 如果您的测试
  • 修改了数据库,请将测试数据库环境设置为您想要进行冒烟测试的环境。
  • 部署和测试。

Yes, you can write smoke tests with Cucumber and Capybara and run them against remote servers. I've done it and it works. I've also done curl/wget and the like on some projects, but Cucumber+Capybara allows you to interact with pages (even ones that use Javascript), not just scrape them.

  • Capybara's Rack::Test driver doesn't support remote requests; its Javascript drivers do. Whether or not you actually need Javascript to work on the pages you're testing, you'll need to use a Javascript driver. Configure Capybara to use a Javascript driver per the driver's docs and tag your tests @javascript. (I recommend the poltergeist/PhantomJS driver; it's faster than Selenium, gives better errors than capybara-webkit, and is easy to set up.) Bonus: you can do things in your tests that need Javascript, and you'll be smoke-testing your entire stack including Javascript.
  • Write your tests so that they either don't need to clean up after themselves or do so in a way that is safe in a production database. They can't use DatabaseCleaner. (To prevent accidents, put the tests in their own project with a Gemfile that doesn't contain DatabaseCleaner.) Since the smoke tests will run against a remote server and therefore can't use transactions to clean up either, they must either not modify the database or must specifically delete only the objects that they create.
  • Set Capybara.app_host = "http://your-server.yourco.com"
  • Set Capybara.run_server = false (not required, but there's no point running a local server that you won't use)
  • If your tests modify the database, set your test database environment to the environment you want to smoke-test.
  • Deploy and test.
拥抱没勇气 2024-11-07 21:01:14

简单地攻击应用程序以查看其是否崩溃的一种方法是从生产 Web 服务器日志文件中获取 GET 请求列表,并将这些请求输入到诸如 curlwget< 之类的程序中。 /code> 将尽快获取它们。

这是一个简单的示例:

#!/usr/bin/env ruby
# rerun

require 'uri'

def extract_from_log(base_uri, log_path)
  File.open(log_path) do |log|
    while (line = log.gets)
      if (line.match(%r{"GET (/\S+) HTTP/\d\.\d"}))
        uri = URI.join(base_uri, $1)
        puts uri.to_s
      end
    end
  end
end

base_uri, log_path = ARGV

if (base_uri and log_path)
  extract_from_log(ARGV[0], ARGV[1])
else
  puts "Usage: #{File.basename($0)} <base_uri> <log_path>"
  exit(-1)
end

给定一个标准 Web 日志文件,其中包含与 "GET /... HTTP/1.1" 匹配的行,您可以轻松提取路径,但有必要提供基本 URI:

rerun http://example.com/ example.log

这将列出输出在该日志文件中找到的所有 URL。您可以将其通过管道传输到 wget 来获取数据:

rerun http://example.com/ example.log | wget -i -

如果您破坏了任何内容,您将开始在应用程序中收到错误,并且通过适当的通知系统,您将能够捕获这些错误并进行跟踪他们下来了。

One way to simply beat on your application to see if it blows up is to grab a list of GET requests from your production web server log file and feed those into a program like curl or wget that will fetch them all as quickly as possible.

Here's a simple example:

#!/usr/bin/env ruby
# rerun

require 'uri'

def extract_from_log(base_uri, log_path)
  File.open(log_path) do |log|
    while (line = log.gets)
      if (line.match(%r{"GET (/\S+) HTTP/\d\.\d"}))
        uri = URI.join(base_uri, $1)
        puts uri.to_s
      end
    end
  end
end

base_uri, log_path = ARGV

if (base_uri and log_path)
  extract_from_log(ARGV[0], ARGV[1])
else
  puts "Usage: #{File.basename($0)} <base_uri> <log_path>"
  exit(-1)
end

Given a standard web log file with lines matching "GET /... HTTP/1.1" you can easily extract the paths, but it's necessary to provide the base URI:

rerun http://example.com/ example.log

This will list out all the URLs found within that log file. You can pipe this to wget for fetching purposes:

rerun http://example.com/ example.log | wget -i -

If you've broken anything, you'll start getting errors in your application, and with a proper notification system you'll be able to catch these and track them down.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文