Cucumber vs. built-in testing? [Rails] - ruby-on-rails

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.

Related

Tool for integration testing to replace Cucumber

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.

How to get started with unit testing in Rails?

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!

TDD? BDD? I'm confused! What should I use for testing and why?

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.

Does cucumber do away with the need to write unit tests?

I am a little confused by the sheer number of testing frameworks available for Ruby/ROR.
I have recently watched the Cucumber Railscasts and found them very interesting. So I started having a play and then struggled to see conceptually where I would put various tests.
It would seem to be quite possible to do everything that can be done in unit tests within Cucumber, so do I need to write unit tests or should I just write my feature definitions and concentrate on providing as good a coverage as I can get using that.
Should I create my Unit tests using Rspec or Test:Unit? When I'm testing Ajax functionality should I use Selenium or Watir?
There seem to be so many options here I am struggling to see which tools to use and where the boundaries are.
What are other peoples experiences of Cucumber and where to draw the line between writing Cucumber Integration tests and Test:Unit and/or Rspec based unit and functional tests. Is anyone aware of a good write-up on this subject suggesting where to draw lines between testing methods and the strengths and weaknesses of the various tool.
I appreciate that some of this is subjective but common approaches on how to attack this issue would be welcomed.
Use Cucumber at a high level to describe what a user should be able to see and do. Use RSpec, Test:Unit, Shoulda, etc. to write unit tests. Straight from the horse's mouth:
When you decide you want to add a new feature or fix a bug, start by writing a new feature or scenario that describes how the feature should work. Don’t write any code (yet).
...
This is when you start writing code. Start by writing a couple of lines of code to address the failure you got from Cucumber. Run cucumber again. Repeat and rinse until you’re happy with your feature. When you get down to nitty gritty details, drop down one abstraction level and use RSpec, or any Ruby testing framework, to write some specs/tests for your classes.
Cucumber is made to test your whole stack, together, as opposed to 'units'.
You need to decide where to draw the line, but a lot of under the hood stuff probably wouldn't be covered in a cucumber test. Say when signing up, I fill out a form, with my name, email, phone number, etc. A unit test might check to see that a new User will also create a new TelephoneNumber. From the user's perspective, they don't really care that it creates a new TelephoneNumber, they care that once they've signed up, they have an account and can see their telephone number.
I don't have too much experience writing cucumber tests (not yet), but I hope this helps a bit.
When a unit test fails (I mean a real unit test that tests a method in isolation using mocks), it tells you what "unit" has a problem. When an acceptance test fails, it tells you what "feature" has a problem, not where the problem is located.
When you create a rails app you get functional, interation, and unit tests by default. Cucumber is an additional test it is a way to also test the experience that your user will have. When they click the button labeled "go" they should see "success" rendered rather than a 404. This will make sure that nothing you do accidentally messes up the user experience, and that from the top to bottom your app works for the most common use cases you can think of. The other tests are meant to insure that nothing goes wrong, and that you have inspected ever model and method with a microscope. It may be possible to replicate unit tests in their entirety with cucumber, but it would be painful (and crazy slow to execute, especially if you're using selenium). The best time to write tests is when you're developing code, and the quickiest and easiest way to do that, is by using in the built-in rails testing and maybe some additional help such as shoulda, rspec, also i'm a huge fan of factory-girl. If you haven't already checked it out www.railscasts.com has a great intro to cucumber, and rspec, and factory-girl, ... I know this question has already been answered (it's no) but this is my two cents. Good luck coding!!
I have thought/struggled with this question much, and here's where I've arrived.
Cucumber first and Cucumber last. Cucumber will provide the primary test coverage.
The core model methods that do the real business work of the application should also be developed/covered with rspec/unit tests.
Why the unit tests as well?
1) The unit tests will test run much faster.
2) This core business logic may (will probably) be used in several ways beyond the current views (which Cucumber tests through). These methods ought to be hammered with all types of possible inputs and outputs directly calling the method in the test.
Why not unit test the rest of the models, and the controllers and views?
1) Cucumber already has it covered once.
2) I find that the views-controller-some-model-methods all work together to get things done (think everything exercised to log in); so I like to test them together.
I've been practicing Cucumber/RSpec for the past half year or so doing BDD.
First of all BDD is not easy to get into, it will feel unnatural at the beginning.
But once you get into it, there's no other way to do programming.
To answer your question. To test Javascript you'll need a javascript driver that can be used by Capybara which is used by Cucumber.
capybara-webkit is what all the cool kids use now these days
There's one important thing to note.
Integration tests are slow.
And unit tests are fast, but can be slow, so it's important you use the right database cleaner and you write good tests that have good isolation.
My test setup which I'm extremely happy with:
Guard for loading spork
Spork for faster tests
Cucumber for integration testing
capybara-webkit for javascript testing
RSpec for unit testing
I don't do view tests and controller tests as these are redundant in my opinion as good knowledge of XPATH willl have you writing remarkable tests that even cover your page layout and structure.
Personally I don't think that you should stop writing unit tests. As an acceptance testing tool, Cucumber should replace your functional tests and, if you writing, view tests.
Cucumber features are supposed to be simple and coupled to the real user's value a given feature has.
From my experience, Cucumber and Rspec have different appeal. Rspec appeals to me from a developer perspective because its easy to write and provides very quick feedback when something breaks. Cucumber does not appeal to me as a developer because it does not run as quickly as Rspec. However, Cucumber does appeal to me as a business stakeholder since it provides full coverage of entire features.
Do yourself a favor and keep writing unit tests.

Why should I use RSpec or shoulda with Rails?

I am setting up a rails app and I just finished making some unit tests and my friend said that apparently fixtures are no longer cool and people are now using RSpec or shoulda. I was wondering what the actual benefits are to use these other toolkits. Any information at all is appreciated.
-fREW
I personally prefer Shoulda to RSpec. I find that Shoulda has less magic syntax than RSpec. My problem with RSpec is that yeah it's very readable when I read it aloud, but when I get to writing it, hmmmm, I'm never sure how a given assertion should be written. Prag Dave explains the problem better than me. He also likes Shoulda and has a few examples.
RSpec and similar frameworks are tooling designed to aid in Behavior Driven Development. They're not just a prettier way to write tests, though they do help with that.
There is plenty of information on BDD here: http://behaviour-driven.org/
And wikipedia: http://en.wikipedia.org/wiki/Behavior_Driven_Development
There are too many benefits to list here, so I'd recommend browsing that site a little.
There are two different things here:
The first thing is what framework to use for writing tests/specs. Here you can choose between Test::Unit, RSpec, Shoulda and so on. The choice is whether you want to do traditional TDD (Test::Unit) or whether you prefer the alternative ways of thinking about specifiying behaviour advocated by developers like David Chemlinsky (RSpec and to some extent Shoulda).
The second thing is how to handle test data. There are Rails fixtures and alternatives desgined with other goals such as the FixtureReplacement plugin. Before Rails 2.0 fixtures had significant and well-documented pratical problems. Many of the practical issues were fixed in Rails 2.0. However fixtures can lead to inadvertent test coupling and some of the alternatives try to avoid this.
RSpec is way more powerful because it's far easier to both read and write tests in. It's also very elegant when using mocks and stubs, a concept which will become extremely useful once you start using them in your tests. Try it in a simple test app (NON RAILS!) and you'll see how elegant your specs are versus the equivalent standard testing.
Check out Josh Susser's The Great Test Framework Dance-off for a comparison of the popular Ruby testing frameworks.
If you are building a large application and don't have a team that are all really good at writing decoupled code that can be well-tested with black box tests and are prepared to fully embrace using/debugging lots of mocks & stubs, don't go down the Factory road.
Wherever you read about how Awesome Factories Are you'll see a little caveat about how factories might not be feasible in a large application because they are a little slower than fixtures.
But "a little slower" is really orders of magnitude slower.
Factories are not significantly easier to code than fixtures that use labels for ids, so long as you keep the fixtures organized. And in some cases factories are harder to debug.
Just tonight I converted a single factory to fixtures, and the runtime of the test file that used it went from 65 seconds to 15 seconds, even though only about 15% of the tests in that test file use that factory.
If you use minitest you can run your tests in random order; this will quickly reveal any data coupling between tests. (not sure if rspec has the option to randomize test order)
Test::Unit is good for small applications. But there are a lot of benefits to use testing frameworks like Shoulda or RSpec, e. g. contexts!!
I don't see Shoulda and RSpec in an either-or-relation. I use Shoulda as a substitute for RSpec when it comes to single-assertion testing. I really like the Shoulda one-liners, but writing matchers is much easier in RSpec. So my advise is to use the different testing tools where they fit best.
You may use testing framework like Cucumber which is even more faster than RSpec..

Resources