I am testing a Ruby Rails website and wanted to get started with Unit and Functional testing.
views:
415answers:
7http://wiki.rubyonrails.org/rails/pages/HowToDoTestDrivenDevelopmentInRails
Beyond this, you should tell us what you've tried. Or what you need to do that's special. Generic answers are easy to find.
Cucumber and RSpec are worth a look. They encourage testing in a behaviour-driven, example-based style.
RSpec is a library for unit-level testing:
describe "hello_world"
it "should say hello to the world" do
# RSpec comes with its own mock-object framework built in,
# though it lets you use others if you prefer
world = mock("World", :population => 6e9)
world.should_receive(:hello)
hello_world(world)
end
end
It has special support for Rails (e.g. it can test models, views and controllers in isolation) and can replace the testing mechanisms built in to Rails.
Cucumber (formerly known as the RSpec Story Runner) lets you write high-level acceptance tests in (fairly) plain English that you could show to (and agree with) a customer, then run them:
Story: Commenting on articles
As a visitor to the blog
I want to post comments on articles
So that I can have my 15 minutes of fame
Scenario: Post a new comment
Given I am viewing an article
When I add a comment "Me too!"
And I fill in the CAPTCHA correctly
Then I should see a comment "Me too!"
My recommendation is (seriously) just bypass the built in rails unit/functional testing stuff, and go straight for RSpec.
The built in rails stuff uses the Test::Unit
framework which ships with ruby, and which is more or less a straight port of JUnit/NUnit/AnyOtherUnit.
I found these frameworks all rather tedious and annoying, leading to general apathy about writing unit tests, which is obviously not what you're trying to acheive here.
RSpec is a different beast, being centered around describing what your code should do, rather than asserting what it already does. It will change how you view testing, and you'll have a heck of a lot more fun doing it.
If I sound like a bit of a fanboy, it's only because I really believe RSpec is that good. I went from being annoyed and tired of unit/functional testing, to a staunch believer in it, pretty much solely because of rspec.
It sounds like you've already written your application, so I'm not sure you'll get a huge bonus from using RSpec
over Test::Unit
.
Anyhow regardless of which one you choose, you'll quickly run into another issue: managing fixtures and mocks (i.e. your test "data"). So take a look at Shoulda and Factory Girl.
Rick Bradley gave a talk at Ruby Hoedown 2008 on precisely this topic. You can get the video at confreaks. If you are not that familiar with mock objects I also recommend Joe O'Brien and Jim Weirich's presentation about flex mock.
Even if the app is already written, I would recommend using RSpec over Test::Unit for the simple fact that no application is ever finished. You're going to want to add features and refactor code. Getting the right test habit early on will help make these alterations less painful
You can also test out the web interface with a Firefox plug-in such as http://selenium.openqa.org/
It will record clicks and text entry and then plays back and will check the page for the correct display elements.