Rails 测试期间发生了什么?
我是 Ruby On Rails 的新手。我喜欢它,它内置了测试功能。但是,我无法全神贯注于测试。这是我关于它的第一个基本问题。
测试过程中到底发生了什么?
我了解开发,我们想要一些结果,我们使用我们拥有的数据或从用户那里获得的数据来实现我们想要的最终结果。但是,测试的概念有时似乎让我感到困惑。我已经在浏览器中测试应用程序一段时间了,我们是否用代码复制相同的内容?这是测试的目的吗?使用自动化代码复制浏览器测试?在此启发我。
I'm new to Ruby On Rails. I love, it has Testing capabilities built in. But, I can't wrap around my head with testing. Here is my first basic Question about it.
What happens during testing really?
I understand development, we want some result, we use the data we have or get it from users to achieve the end result we want. But, the notion of testing seems sometimes confusing for me. I have been testing applications in browser for some time, are we replicating the same with code? Is it what testing is about? Replicating browser testing with automated code? Enlighten Me here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
阅读测试 Rails 应用程序指南将是一个很好的起点。
基本上,您有三种类型的测试:单元、功能和集成。
单元测试正在测试您的模型。在这些测试中,您检查模型的单个方法是否按预期工作,例如您设置分配带有空格的登录名,然后测试空格是否被删除:
功能测试正在测试您的控制器(和意见)。在每个测试中,您使用一组给定的参数模拟发送到一个控制器的一个请求,然后确保控制器返回正确的响应。
但请注意,在此测试中您无法测试页面的渲染,因此它并不是严格模拟浏览器。要测试您的页面是否美观,您需要手动执行此操作(我几乎确定存在一些技术,但我不知道它们)。
功能测试的一个示例:
集成测试正在测试一系列请求 - 类似于用户登录、获取“创建用户”页面、创建用户等场景。这些测试检查单个请求(在功能测试中测试)是否能够协同工作。
我看到 Simone 已经指出了测试中自动化的重要性,因此指南的链接是我的答案中唯一的价值;-)
您可能会发现应用 测试驱动开发,尤其是当您的项目稍微成熟时。
我知道通过编写测试启动项目并不容易,因为通常你还不知道一切将如何工作,但是稍后,当你发现错误时,我强烈建议启动修复编写失败的测试用例中的每个错误。它确实非常有助于修复错误阶段以及之后的工作——确保错误不会再次出现。
好吧,我注意到我没有直接回答你的问题;-)
当你开始测试过程时,Rails:
test/fixtures/*
),test/units/*
的所有测试类和其他目录,test "should Something.."
创建(按字母顺序排列,但您可能认为顺序是随机的),setup< /code> 过程,每次调用后它都会执行
teardown
过程,您将在指南中找到更多信息。
Reading A Guide to Testing Rails Applications will be a good starting point.
Basically, you have three kinds of tests: unit, functional and integration.
Unit tests are testing your Models. In these tests you check whether a single method of your model works as expected, for example you set assign a login with spaces, and then you test whether the spaces were removed:
Functional tests are testing your controllers (and views). In each test you simulate one request sent to one controller with given set of parameters, and then you ensure that the controller returned the proper response.
Note however, that in this test you cannot test the rendering of the page, so it's not strictly simulating a browser. To test whether your page looks nicely, you need to do it manually (I am almost sure some techniques exist, but I do not know of them).
An example of functional test:
Integration tests are testing a sequence of requests - something like a scenario, where an user logins, gets the 'create user' page, creates an user, and so on. These tests check whether the single requests (tested in functional tests) are able to work together.
I see that Simone already pointed the importance of automation in tests, so the link to the Guide is the only value in my answer ;-)
You may find it very helpful to apply some rules of Test Driven Development, especially when your project matures a little.
I know that it's not easy to start the project by writing test, because often you do not yet know how everything will work, but later, when you find a bug, I strongly suggest to start fixing every bug from writing a failing test case. It really, really helps both in the bug-fixing phase, and later - ensuring that the bug does not reappear.
Well, I noticed that I did not directly answer your question ;-)
When you start test procedure, Rails:
test/fixtures/*
)test/units/*
and other directories,test "should something.."
(alphabetically, but you may consider the order as being random)setup
procedure, and after every call it executesteardown
procedure,You will find more information in the Guide.
测试期间发生的情况是,您实际上运行一组专门的程序或例程(测试代码),它们调用应用程序中的例程(被测代码)并验证它们是否产生预期结果。测试框架通常具有某种机制来确保每个测试例程独立于其他测试。换句话说,一项测试的结果不会影响其他测试的结果。
具体来说,在 Rails 中,您可以使用
rake test
命令行工具运行测试。这将以随机顺序加载并执行每个测试例程,并告诉您每个测试是否成功。What happens during testing is that you really run a set of specialized programs or routines (test code) that calls routines in your application (code under test) and verifies that they produce the expected results. The testing framework usually has some mechanism to make sure that each test routine is independent of the other tests. In other words the result from one test does not affect the result of the others.
In Rails specifically you run the tests using the
rake test
command line tool. This will load and execute each test routine in a random order, and tell you if each test was successful or not.这个答案不一定适用于 Rails 本身。当您谈论 Rails 中的测试时,通常指的是自动测试。
自动这个词的本质就是这个意思。这实际上是单元测试和“浏览器”测试之间最大的区别。
通过单元测试,您本质上是编写一段代码,一个例程,强调代码的特定部分,以确保其按预期工作。与“浏览器”测试相比,单元测试的主要优点是:
这是一个非常简单的基本示例。采取一个模型,假设是用户模型。您具有以下属性:
first_name
、last_name
。您需要一个名为name
的方法来返回名字和姓氏(如果存在)。这是方法
,这是相应的单元测试。
This answer doesn't necessary apply to Rails itself. When you talk about testing in Rails, you usually mean automatic testing.
The word automatic is the essence of the meaning. This is in fact the biggest difference between unit testing and "browser" testing.
With unit testing you essentially write a code, a routine, that stresses a specific portion of your code to make sure it works as expected. The main advantages of unit testing compared to "browser" testing are:
Here's a basic, very simple example. Take a model, let's say the User model. You have the following attributes:
first_name
,last_name
. You want a method calledname
to return the first and last name, if they exist.Here's the method
and here's the corresponding unit test.