怎么单独进行一个单元测试

How to Run a Single Rails Unit Test

May 28th, 2010 | Comments

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/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值