怎么单独进行一个单元测试
来源:互联网 发布:iphone赚钱软件 编辑:程序博客网 时间:2024/04/28 10:27
How to Run a Single Rails Unit Test
This post explains how to execute a single unit test (or even a single testmethod) instead of running the complete unit test suite.
In order to run the unit tests of your rails application, basically you havethese official possibilities:
rake test
: runs all unit, functional and integration tests.rake test:units
: runs all the unit tests.rake test:functionals
: runs all the functional tests.rake test:integration
: runs all the integration tests.Each one of these commands requires some time and they are not the bestsolution while developing a new feature or fixing a bug. In this circumstancewe just want to have a quick feedback from the unit test of the code we areediting.
Waiting for all the unit/functional tests to complete decreases ourproductivity, what we need is to execute just a single unit test. Fortunatelythere are different solutions for this problem, let’s go through them.
The easy approach: use your favorite IDE
Most of the IDE supporting ruby allow you to run a single unit test. If youare using Netbeans running a single unit test is really easy:
- make sure the editor if showing the file you want to test or the file containing its unit tests
- Hit Ctrl+Shift+F6 or click on the following menu entry: Debug->Debug Test FileTwo new windows will be opened: one will contain the output produced by yourunit test, the other one will show the results of the unit test.
As you will notice the summary window contains also some useful informationlike the:
- hyper links to the exact location of the code that produced the error/failure.
- execution time required by each one of the test methods.As you will experience it will be like “compiling” your ruby code.
From the console
If you are not using Netbeans you can always rely on some command line tools.
No additional tools
These “tricks” don’t require additional gems, hence they will work out of thebox.
The first solution is to call this rake task:
rake test TEST=path_to_test_file
So the final command should look like
rake test TEST=test/unit/invitation_test.rb
Unfortunately on my machine this command repeats the same test three times, Ihope you won’t have the same weird behavior also on your systems…
Alternatively you can use the following command:
ruby -I"lib:test" path_to_test_file"
It’s even possible to call a specific test method of your testcase:
ruby -I"lib:test" path_to_test_file -n name_of_the_method"
So calling:
ruby -I"lib:test" test/unit/invitation_test.rb - test_should_create_invitation
will execute only InvitationTest::test_should_create_invitation.
It’s also possible to execute only the test methods matching a regularexpression. Look at this example:
ruby -I"lib:test" test/unit/invitation_test.rb -n /.*between.*/
This command will execute only the test methods matching the /.between./regexp.
Using the single_test gem
If you want to avoid the awful syntax showed in the previous paragraph there’sa gem that can help you, it’s calledsingle_test.
The github page contains a nice documentation, but let’s go through the mostcommon use cases.
You can install the gem as a rails plugin:
script/plugin install git://github.com/grosser/single_test.git
single_test will add new rake tasks to your rails project, but won’toverride the original ones.
Suppose we want to execute the unit test of user.rb, just type the followingcommand:
rake test:user
If you want to execute the functional test of User just call:
rake test:user_c
Appending ”c”_ to the class name will automatically execute its functionaltest (if it exists).
It’s still possible to execute a specif test method:
rake test:user_c:_test_name_
So calling:
rake test:user_c:test_update_user
Will execute the test_update_user method written inside oftest/functional/user_controller_test.rb.
It’s still possible to use regexp:
rake test:invitation:.*between.*
This syntax is equivalent to ruby -I"lib:test" test/unit/invitation_test.rb-n /.*between.*/
.
Possible issues
When a single unit test is run all the usual database initialization tasks arenot performed. If your code is relying on newly created migrations you willsurely have lots of errors. This is happening because the new migrations havenot been applied to the test database.
In order to fix these errors just execute:
rake db:test:prepare
before running your unit test.
http://flavio.castelli.name/2010/05/28/rails_execute_single_test/
- 怎么单独进行一个单元测试
- openstack项目怎么进行单元测试
- android 怎么进行调试程序 如何采用AndroidTestCase进行单元测试
- 单元测试(对项目中某一个独立业务模块进行单独的测试)
- 【遇到的问题】动态addview之后怎么识别每个单独的view进行对应的操作
- 进行单元测试
- 怎么对一个字符串进行url编码
- 单独一个文件
- 单独编译一个ipk
- 利用JUNIT进行单元测试
- 如何进行单元测试
- 使用NUnit进行单元测试
- 利用J2MEUnit进行单元测试
- 使用CppUnit进行单元测试
- 利用J2MEUnit进行单元测试
- 如何进行单元测试
- 如何进行单元测试
- 用VS2005进行单元测试
- md模块浅谈(二)md事件
- 开发人员一定要加入收藏夹的网站
- C# 集合
- HDOJ1022
- 字符串中连续出现最长子串
- 怎么单独进行一个单元测试
- 分类
- android 开机动画(转载)
- result type
- 20非常有用的Java程序片段
- java wait()和sleep()方法之经验谈
- GRUB入门教程
- 设计模式-Adapter
- WifiManager自动连接wifi接入点