如何开始测试 Rails 应用程序?

发布于 2024-10-24 19:47:59 字数 240 浏览 1 评论 0原文

我想为我的 Rails 3 应用程序编写一些自动测试。

我想知道如何开始。

我听说过 Selenium/RSpec/Cucumber,我想还有更多选择。

这些测试框架的优点/缺点是什么?其中哪一个拥有最好的文档? Ruby 世界中哪一个最受欢迎?而在这个行业呢?

我了解如何编写测试的一般知识。我只是想学习用于测试 Rails 应用程序的适当测试框架。

请帮助我决定从哪个测试框架开始。

I would like to write some automatic tests for my Rails 3 application.

I wonder how to start with that.

I've heard about Selenium/RSpec/Cucumber, and I guess there are many more options.

What are the advantages/disadvantages of these testing frameworks ? Which of them has the best documentation ? Which one is the most popular in the Ruby world ? And in the industry at all ?

I have the general knowledge of how to write tests. I just want to learn the appropriate testing framework(s) for testing Rails applications.

Please help me to decide with which testing framework to start.

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

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

发布评论

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

评论(5

何其悲哀 2024-10-31 19:47:59

有许多工具可以从许多不同的方面测试 Rails 和其他 Web 应用程序。但如果您是测试新手我强烈建议您先学习 Rails 自己的测试框架,然后再开始使用其他工具。

学习并稍后掌握一个测试框架可以使将来更轻松地了解其他框架的优缺点并使它们协调一致地工作。

您可以从测试以下内容开始:

  • 对模型进行单元测试
  • 对控制器进行功能测试
  • 了解夹具以及如何加载测试数据

我见过许多失败的测试工作,但我从未见过它们因为选择了错误的工具而失败/框架。他们失败是因为他们不知道如何掌握他们使用的工具,也不知道如何充分了解测试的基础知识。

在此处阅读有关 Rails 测试的更多信息。
http://guides.rubyonrails.org/testing.html

手动探索性测试< /strong>
尽管我很喜欢自动化测试,但恕我直言,它并不能替代手动测试。主要原因是自动化设备只能执行被告知的操作,并且只能验证其被告知的内容以视为通过/失败。人类可以利用其智能来发现错误并提出测试其他事物时出现的问题。
在我的另一个答案中阅读有关混合自动和手动测试的更多信息:
您开发网站时使用哪些测试方法?

There are many tools for testing rails and other webapps from many different aspects. But if you are new to testing I highly recommend you start with learning Rails own testing framework before start using other tools.

Learning, and later mastering, one testing framework makes it easier in the future to understand pros/cons with other framework and make them work in unison.

You could start with testing the following things:

  • Unit Testing your Models
  • Functional Tests for Your Controllers
  • Learning about Fixtures and how to load test data

I have seen many failed testing efforts, but I never saw them fail because they choose the wrong tool/framework. They fail because they don't know how to master the tools they use, and learn enough about the basics about testing.

Read more about Rails testing here.
http://guides.rubyonrails.org/testing.html

Manual Exploratory Testing
As much as I love automated testing it is, IMHO, not a substitute for manual testing. The main reason being that an automated can only do what it is told and only verify what it has been informed to view as pass/fail. A human can use it's intelligence to find faults and raise questions that appear while testing something else.
Read more about mixing Automated and Manual Testing in another of my answers here:
What test methods do you use for developing websites?

岁月苍老的讽刺 2024-10-31 19:47:59

我不会说有任何一套最好的测试工具;社区肯定还没有以任何方式决定任何标准。

我强烈推荐实用程序员的The RSpec Book——大约一半这本书是关于使用 Cucumber、RSpec 和 Selenium 等浏览器模拟器对 Rails 进行测试的。它全面概述了您在使用 Rails 时会遇到的不同情况以及在每种情况下您可能想要使用的工具。

I wouldn't say there is any one best set of testing tools out there; and the community definitely has not decided on any being standard in any way.

I highly recommend The RSpec Book from the Pragmatic Programmers-- about half of the book is on testing with Rails with Cucumber, RSpec, and browser simulators like Selenium. It's a comprehensive overview of the different situations you will find yourself in with Rails and what tools you might want to use in each situation.

£噩梦荏苒 2024-10-31 19:47:59

Selenium 是一个自动测试用户界面的框架。 Selenium 脚本启动浏览器窗口,转到网页并操作页面元素。

RSpecCucumber 是所谓的“行为驱动开发 (BDD)”的工具。 BDD 是一个开发过程,您可以在其中描述单独的功能,以及检查此功能的一些脚本(可以是 Selenium 脚本)。 RSpec 和 Cucumber 基本上连接了这两者,因此当您运行测试脚本时,您会看到用简单语言编写的功能列表及其测试状态。

Selenium 脚本可以用多种语言编写,而 Cucumber 脚本则使用 Ruby。文档对所有这些都可以,但需要一些编程语言知识。

起始链接:

Selenium is a framework to automate testing of user interface. Selenium script launches a browser window, goes to a web-page and manipulates page elements.

RSpec and Cucumber are tools for what is called "behavior driven development (BDD)". BDD is a development process where you have description of a separate feature, and some script (can be a Selenium script) that checks this feature. RSpec and Cucumber basically connect these two, so that when you run a test script, you see the list of features written in plain language and their testing status.

Selenium scripts can be written in a number of languages, while Cucumber scripts use Ruby. Documentation is OK for all of them, some knowledge of programming language is required though.

Starting links:

凡尘雨 2024-10-31 19:47:59

最简单的入门方法是使用 TestUnit。当您使用脚手架生成模型、视图、控制器时,它会自动为您生成一个测试文件夹,其中包含测试环境设置文件和测试,或者您可以使用 Rails 生成器将测试添加到现有模型。

然后就是阅读 TestUnit 上的文档,其中有相当多的文档。

下一步,我个人更喜欢使用 RSpec 而不是 Cucumber - 我发现 Cucumber 很难维护,虽然拥有老板可读的测试很棒,但我发现大多数老板并没有真正做到这一点。想要阅读或为测试做出贡献,这会导致开发非常耗时,而且非常“小”——大量的小文本分散在各个文件中。

如果您想立即开始使用 RSpec for Rails,那么我建议 Rspec-Rails,其中包括许多用于断言应用程序正确行为的帮助程序。

我可以继续说下去,但你问了“我从哪里开始”——有两个好地方。

The easiest way to get started is using TestUnit. When you generate a model, view, controller using scaffolding it will automatically generate a testing folder for you containing a test environment setup file and tests, or you can add tests to existing models with a rails generator.

It is then a matter of reading the documentation on TestUnit, of which there is a fair amount.

For a next step, my personal preference is to use RSpec over Cucumber - I've found Cucumber hard to maintain, and whilst it is wonderful to have tests that are readable by your boss, I've found that most bosses don't really want to read or contribute to tests, and it makes for time-consuming development and is very 'bitty' - lots of small bits of text scattered in various files.

If you want to jump right in and begin using RSpec for Rails, then I'd recommend Rspec-Rails, which includes many helpers for asserting the correct behaviour of your application.

I could go on, but you've asked for 'where do I start' - there's two good places.

£冰雨忧蓝° 2024-10-31 19:47:59

有很多框架可以帮助您测试 ruby​​/rails 代码...最棒的是 Ruby 的动态特性为您提供了很大的灵活性。

我喜欢使用 RSpec 来测试代码的内部结构,并使用 Cucumber 来测试应用程序的行为。所以一般来说,RSpec会测试模型和方法,而Cucumber则通过浏览器测试用户交互。

我强烈推荐这本关于在 Cucumber 上使用 RSpec 的 BDD 的实用程序员电子书。

There are a bunch of frameworks that help you test your ruby/rails code... the great thing is the dynamic nature of Ruby gives you a lot of flexibility.

I like to use RSpec to test the internals of my code, and Cucumber to test the application's behavior. So generally, RSpec will test models and methods, and Cucumber tests user interaction through the browser.

I highly recommend this pragmatic programmers ebook on BDD using RSpec on Cucumber.

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