cipele46 / cipele46-web

4 stars 0 forks source link

Fast tests #4

Closed shime closed 11 years ago

shime commented 11 years ago

We should embrace and enforce TDD!

It means you write tests before you write code. It means you run unit tests before you make any change. It means slow tests slow you down and might even do you harm. If they're slow it's a pain to run them.

A test is a unit test if it tests something in an isolation.

A test touching a DB is an integration test. A test requiring ActiveRecord is an integration test. A test requiring Rails is an integration test.

A test requiring only a class that encapsulates your business logic is a unit test.

Each of your unit tests should be independent. Order should not matter. Isolation encourages focus.

10 seconds wait for unit tests is UNACCEPTABLE!

Unit tests should be:

What have I done:


"Give me some numbers already!"

Before

time rspec spec/models
rspec spec/models  8.10s user 0.58s system 57% cpu 14.982 total

After

time rspec spec/models
rspec spec/models  0.44s user 0.05s system 99% cpu 0.500 total

So basically, tests are 30 times faster now.


I'm not pulling all these arguments out of thin air. Here are the resources:


Thoughts? Opinions? Good to merge?

antebarac commented 11 years ago

Respect.

Merge.

Ante Barać

On 22. 7. 2013., at 16:34, Hrvoje Simic notifications@github.com wrote:

We should embrace and enforce TDD!

It means you write tests before you write code. It means you run unit tests before you make any change. It means slow tests slow you down and might even do you harm. If they're slow it's a pain to run them.

A test is a unit test if it tests something in an isolation.

A test touching a DB is an integration test. A test requiring ActiveRecord is an integration test. A test requiring Rails is an integration test.

A test requiring only a class that encapsulates your business logic is a unit test.

Each of your unit tests should be independent. Order should not matter. Isolation encourages focus.

10 seconds wait for unit tests is UNACCEPTABLE!

Unit tests should be:

Thorough Stable Fast Few What have I done:

removed dependency on spec_helper which loads up the Rails env, which which adds about 10s to any test that's being ran removed pending unit tests because they only require spec_helper and therefore just add to the boot time externalized business logic into modules located in /lib/extensions "Give me some numbers already!"

Before

time rspec spec/models rspec spec/models 8.10s user 0.58s system 57% cpu 14.982 total After

time rspec spec/models rspec spec/models 0.44s user 0.05s system 99% cpu 0.500 total So basically, tests are 30 times faster now.

I'm not pulling all these arguments out of thin air. Here are the resources:

Sandi Metz - Magic Tricks for Testing : https://speakerdeck.com/skmetz/magic-tricks-of-testing-railsconf Sandi Metz - Solid Object Oriented Design : https://speakerdeck.com/skmetz/solid-object-oriented-design Avdi Grimm - Objects on Rails : http://objectsonrails.com/ Corey Haines - Fast Rails Tests : http://arrrrcamp.be/videos/corey-haines/fast-rails-tests Tom Clemens - 4 Steps to Faster Rails Tests : http://tom-clements.com/blog/2011/10/23/4-steps-to-faster-rails-tests/ Thoughts? Opinions? Good to merge?

You can merge this Pull Request by running

git pull https://github.com/cipele46/cipele46-web fast-tests Or view, comment on, or merge it at:

https://github.com/cipele46/cipele46-web/pull/4

Commit Summary

potraznja > trebam | pounuda > poklanjam add default rake task use zeus remove integration tests from unit test for user add unit tests for ad order of magnitude faster unit tests prettify rspec output add delegation test for ad Rakefile should remain slow add ad_creation service tests File Changes

M Rakefile (5) M app/admin/ads.rb (2) M app/helpers/for_select_helper.rb (4) M app/models/ad.rb (47) M app/models/user.rb (6) M app/services/ad_creation.rb (9) M app/views/application/_left_navigation.html.erb (10) M config/application.rb (2) M db/seeds.rb (50) A lib/extensions/ad/delegation.rb (9) A lib/extensions/ad/expiration.rb (17) A lib/extensions/ad/status.rb (25)
A lib/extensions/ad/type.rb (34) A lib/extensions/user/naming.rb (9) M spec/models/ad_spec.rb (98) D spec/models/admin_user_spec.rb (5) D spec/models/blog_spec.rb (5) D spec/models/category_spec.rb (5) D spec/models/city_spec.rb (5) D spec/models/favorites_spec.rb (5) D spec/models/region_spec.rb (5) M spec/models/user_spec.rb (110) A spec/services/ad_creation_spec.rb (29) M spec/spec_helper.rb (3) A zeus.json (21) Patch Links:

https://github.com/cipele46/cipele46-web/pull/4.patch https://github.com/cipele46/cipele46-web/pull/4.diff

shime commented 11 years ago

Thanks, merged.