I want to learn how to write unit tests for a Rails App. Where do I start? All the tutorials on Google are old, back from 2007, and there seem to be an abundance of choice but not a preferred solution.
Right now most people seem to be using Rspec for unit testing and Cucumber for integration testing. You can see a fairly recent poll here where 87% chose Rspec in a survey.
A great book for Rspec and Cucumber is The Rspec Book written by the current maintainer of Rspec. It goes over both Rspec and Cucumber.
Railscasts also has a few relevant screencasts. Cucmber1, Cucumber2
Many people seem to love Cucumber but it doesn't seem as useful if you don't have a client you're doing work for. Most of my projects are side projects I do myself so these days I'm looking into Steak instead of Cucumber. This will allow me to use Rspec for unit tests and Steak for integration tests and reduces some complexity/tedium that is introduced when using Cucumber.
People have said that it doesn't really matter which testing framework you pick, it's more important that you START TESTING. I agree with that but hopefully these resources will help you get started.
However, one thing you probably want to avoid from the very start are fixtures. Use factories instead and check out this Railscasts episode on it.
UPDATE: Steak is no longer necessary and the same functionality is baked into Rspec.
I'd suggest starting with the Rspec Book by David Chelimsky and friends. It goes over the various types of testing, why you should test and runs through various examples that show you good practices as well as giving you guidelines for what to avoid.
Rspec is very popular and preferred by many over the default rails test suite. Most material I've read that goes over using the default test suit ends with an introduction to Rspec and how it makes life simpler.
If you want to do it the proper way, you should use others testing frameworks like Rspec, Cucumber or shoulda. This way you will write Unit Tests in a BDD context. Those frameworks are recent so obviously the documentation that you will find about them are recent enough! Finally, I'd suggest you to read this great blog post from Sarah Mei about the outside-in approach combining Rspec/Cucumber, that's how I was convinced to use these frameworks, she really explains very well how you should approach the tests!
Related
Now I'm using cucumber, but I find it a little oververbosity. I'm not going to show cucumber futures to somebody without Ruby knowledge. So now I'm thinking that it isn't necessary to duplicate integration test in human-readable text and in step definitions. It makes me to do useless job.
I'm in the middle of my way to something more geeky than cucumber. Something like rspec integration but with power no less than cucumber power.
Could you advice some tool for integration testing for my purposes?
Try Spinach :)! I have the feeling it is exactly what you are looking for!
RSpec feature specs are fairly analogous to Cucumber integration tests. This requires the Capybara gem.
Moving your tests back to RSpec keep them more code-like, and you remove the extra step of needing to parse gherkin. This is a good approach if you don't really need to be sharing the specs with non-technical folks.
Thoughbot has a post that illustrates an interesting actor-based approach to feature specs: End-to-end testing with RSpec integration tests and Capybara.
The upcoming Rails 4 in Action book describes how to do feature specs in great detail. There is a GitHub repo for the code referenced in the book (the sample app is a project-tracking system) if you want to take a look.
For the sake of completion, you should also consider turnip, though it seems very similar to spinach; deciding factor for me for choosing turnip was the seeming lack of placeholders in Spinach, which I have a hard time understanding.
So yeah, I'm gettin' into this testing thing!
But I don't know what to use =/
Rspec + Shoulda?
Rspec + Steak?
Minitest?
Cucumber?
Capybara?
Coulda? (Cucumber with Shoulda mixup)
Mini_shoulda? (Minitest with Shoulda mixup)
Argh, so many choices! I'm confused <.< I do know I want something small and simple to test my future gems and sinatra & rails apps. What do you people use and why?
If you are just starting out, you should go with the defaults and follow the official guides. The important thing is that you practice testing. It's less important which testing library/framework you use - that's a stylistic choice that's secondary to actually practicing testing in the first place - and the easiest way to get starting testing your Rails app is to follow the "Golden Path" that the official guides lay out.
Once you get more comfortable with testing your Rails app, you will be in a much better position to evaluate the options out there. I personally like rspec and cucumber (at the moment - I may decide to change my preference), but I was only able to come up with this preference after already being somewhat familiar with testing and after already being somewhat familiar with Rails.
I just came across this blog post which states:
It seems that the accepted way to test in Ruby is to use Rspec for unit tests and to use Cucumber for acceptance tests (higher level functional testing).
It then goes on to ask: "if we agree that BDD is good, why don’t we write our unit tests in a format that is more amenable to BDD, that being the Cucumber format of tests?" and compares rspec unit tests against unit tests in Cucumber.
At work we use the pretty standard RSpec and Cucumber combination. I don't know how they decided on this specific combination, it was already there when I started and worked for us, so there was no need to change. Also it's a quite widely use combination so finding examples with Google etc. isn't too hard.
For my 1.9 based private projects I think I will use Minitest from now on. No external dependencies, a simple BDD DSL (require 'minitest/spec') and some other niceties like randomization. Here's a nice quick intro:
http://www.bootspring.com/2010/09/22/minitest-rubys-test-framework/
For someone just getting started I recommend just using RSpec. It doesn't do everything but it will allow you to build up reasonable sets of unit and integration tests. When you run into the limits of what RSpec handles easily then let that need guide you in choosing additional tools.
Can't offer anything more sensible than Justice, but before you ignore that wisdom :-) do check out this video (minitest author Ryan Davis at Cascadia 2011 ruby conf):
http://confreaks.net/videos/618-cascadiaruby2011-size-doesn-t-matter
slides: http://www.zenspider.com/~ryan/presentations/CascadiaRubyConf_2011_-_Size_Doesn%27t_Matter.pdf
Being able to run all your tests very quickly is a very good thing.
I have been trying to determine a good way to test rails apps, covering all relevant aspects... since I have no experience writing tests at all I have been looking at railscasts.com which seems like a pretty good resource to learn basic stuff for rails. but for tests I have found several episodes, some using rails unit test classes, others use Cucumber, RSPEC, ZenTest, Autotests, Webrat, Selenium, etc.
So Im now just not sure if Im supposed to pick one of these or a combination. are some of these now old and obsolete?? are they mutually exclusive?... basically I want to learn testing for rails and would like to hear opinions on how to properly and sufficiently test rails apps and what framework to use for the different types of tests
please help, thanks in advance
ps. if someone can provide a good resource for learning the difference between unit tests, integration tests and so on I'd appreciate it.... I know I didnt do TDD or BDD cause my app is pretty much done now, and all seems to be working just fine. but I still would like to add tests for learning purposes and future developments.
http://guides.rubyonrails.org/testing.html
I asked a question about different testing frameworks yesterday. This question can be found here. Now that I have a better understanding of the different frameworks, I have a very simple question:
With a basic understanding, but very limited experience with writing tests with rails' built in testing framework (basic assertions), would it be okay for me to jump directly to testing with RSpec, Webrat, and Cucumber?
Thank you!
As a side note: yes, this is an opinion based question, but I feel that the input received to this question is valuable enough to the community to keep this question open. Thanks.
I never wrote a lot of tests with the build in testing framework, but jumped right in to use Cucumber with factory girl and RSpec and am VERY happy with it. I find it very easy to describe features I want to work on in Cucumber first. I always had trouble getting myself to write Unit tests first.
So from my personal experience, I can encourage you to jump right into the fun stuff!
Using other testing framework, you can write test easier and easier to maintain, some test frameworks build-in "macro" is very helpful. And what's more you can extend these testing framework like shoulda you can write your own "macro".
Using cucumber like BDD framework, you can write clean integration test. You can even tell your BA how to run it. it will helpful to your tester too.
Now what we use is shoulda, rr mock, cucumber + selenium
Cucumber is for behavior, e.g. integration tests. Test::Unit and Rspec are more appropriate for unit tests that test the code. It's well worth spending a few hours reading up on unit, integration, and acceptance testing to understand the different purposes of each kind of test. This is a big field and it's a good investment.
Once you've done that, yes, by all means jump in with Rspec and Cucumber. You might also look at Turnip. For JavaScript, consider Capybara.
Everyone is talking about TDD (BDD) in Rails (and not just Rails) development world today. It's easy to find plenty of good general information about it, there are quite a few tools you can use for this purpose and there are many (good) examples of how to use them.
Now, I'm already on the train. I like the idea (never did TDD before) and I decided to go for Shoulda (because it seems to me quite easy to understand). I read plenty of examples and have done some first tests. My problem is this: I'm just not sure if I'm doing it right. As I said, there are many tiny examples all over the net, but I haven't found any comprehensive guide covering overall workflow of TDD with Shoulda.
Here are my questions:
What is your way of practising TDD with Shoulda? (What is your usual workflow throughout one iteration?)
What other tools do you use (except of Shoulda itself)?
Any other tips?
I'm using RSpec instead of shoulda, (although I believe that shoulda can be used with RSpec) and the biggest improvement to my workflow has come from using Autotest and then RSpactor.
These tools automatically monitor your source tree for changes and execute the relevant tests if update either the test or implementation file. A pretty small change to the testing practice but the immediate feed back has increased my discipline in writing tests for every piece of functionality.
+1 for Autotest.
If you have a large test suite and only want to run the test you are working on (as opposed to the whole test suite) check out this monkey patch for Autotest.
As for other tools you should check out Factory Girl as a fixtures replacement.
Most of what I know about using Shoulda came by reading others test code. Check out some of Thoughtbot's projects on github, such as Clearance and Pacecar.
I totally agree that there is a significant gap in the rails BDD documentaion. Lots of little examples but no big ones giving an overall picture of how to do it. Even the books that have chapters on testing are guilt of this. The only full examples I have seen are peepcode's three part screencast on rspec and a video on confreaks about flex mock.
I'd love to hear about other good resources on BDD that go beyond minor examples of syntax and I'd really like see a book on the topic.