Is Test::Unit still relevant in rails?
Asked Answered
M

5

17

I am learning Rails the age old way. By reading Agile Web Development with Rails (3rd Edition) as a starting point. I am currently in the chapter that teaches Testing. I am also aware of other BDD Testing framework such as RSPec. So I was wondering if frameworks such as RSpec, Cucumber, Shoulda replace the need for knowing/using Test::Unit? Are they both relevant in their own right and used for different purposes?

UPDATE: Since I am new to Testing, I would also love to get feedback on what resources are useful to get my feet wet with different frameworks.

Thanks!

Mccullum answered 4/4, 2009 at 14:48 Comment(4)
Hi, how did you get started with shoulda and did you decide that?Isopropanol
Angela, I do not have enough experience in Rails & the testing frameworks. I do not have a preference of the tools either. Those were the frameworks I knew of & wanted to get a sense from people about why people are moving away from Test::Unit.Mccullum
That is the best book I have found so far...and some people say that it is best to start with Test::Unit.Amen
I think after you digest Agile Web Development book you might like Hartl's book "Ruby on Rails Tutorial". I found his book much more difficult than Agile Web Development. But it might be good because it uses RSpec, Git, GitHub, Autotest, etc. Also, I think you need a Mac to get all the gems to work...Amen
S
16

The world has changed! Check down and upvote @prusswan

I think Test::Unit is still relevant even though there is a lot of hype surrounding BDD and other testing tools.

That being said, if you are up for it, you could bypass learning Test::Unit and start with something like RSpec and Shoulda right away, but there is something to be said about following through the examples in the Agile Web Development Book to see where the ideas from BDD came from.

I find myself still using Test::Unit for some projects since it comes with Rails and is still a very great testing framework.

So long story short, I don't think it's obsolete but it's not the cutting edge any more. BDD is a testing paradigm shift especially if you start using Cucumber and Webrat, but it's fantastic once you get into it. Shoulda is the easiest to make the transition to, so I would start with Test::Unit, then move to Shoulda, then Give RSpec and Cucumber a try.

You are testing or at least interested in testing! That's the best part. In the end it doesn't matter what you use as long as you are happy with it.

Good luck!

Sipes answered 4/4, 2009 at 15:3 Comment(4)
Thanks! I think testing is great. It's not that I do not like Test::Unit but it seems that as I am learning it, I can already see how much recoding tests would need with minor changes in code. I can envision having to constantly write fixtures with changes in the model.Mccullum
Try out Factory_Girl from Thoughtbot. It's the best way to manage Fixtures when testing.Sipes
Even better than Factory Girl is Machinist. github.com/notahat/machinist/tree/masterAlmonry
It's worth pointing out that three years have passed for those who are finding this question -- it may be that @prusswan's answer is closer to where things have landed.Homosexual
D
6

Just thought I would post an update from 2012:

Out of the three main and current (usable with Rails 3.2 with no more than some minor modifications) introductory books on RoR, two of them are using Rspec (with at most a short mention for Test::Unit) and one of them is planning on even moar Rspec for the next edition, so it is clear who would be the winner of this "battle" for now

And allow me to selectively quote dhh:

The important thing is of course that we get people testing, so tools shouldn't matter too much.

Revisit in 2014:

Minitest has now superceded Test::Unit as a default in the current versions of Rails. Personally I feel it deserves a closer look compared to its predecessor for a number of reasons -

  1. Its status as a default
  2. Better support for capybara and selenium, a large part due to gems like this
  3. Relatively lightweight
  4. Mostly backward compatible with Test::Unit
Dade answered 4/4, 2009 at 14:48 Comment(1)
Agree that it looks like as of late 2012, Rspec is kind of the default, with FactoryGirl as a replacement for fixtures, and Capybara to better test views and navigation. There's much to be said for picking one and sticking with it until something demonstrably better comes out -- then it's easier for us all. But it looks like RSpec, at least, is where it's at today.Homosexual
H
4

Rspec is an entirely separate testing framework. Shoulda is an enhancement to Rails's built-in framework, Test::Unit. If you're using Shoulda, you're using Test::Unit but with more capabilities and simpler, more readable syntax.

I've tried Rspec and Shoulda and for me, Shoulda wins hands down. I like it way better. But that may be a matter of taste.

Note that you can use Cucumber with Shoulda.

Other resources? I recommend the ZenTest and RedGreen gems. ZenTest provides autotest, which lets you run your tests automatically every time you change a file. It's a big help.

Regarding fixtures vs. factories, if you need to set up a bunch of interrelated objects where you're testing both sides or a parent-child relationship and/or testing many-to-many relationships, fixtures work a lot better. Actually I'm not even sure you can do that with factories. So don't dismiss fixtures -- they have their uses.

Hurtless answered 4/4, 2009 at 14:48 Comment(1)
I disagree, it is easy with Factories and it is so much easier to maintain, fixing fixtures is a nightmare, whereas with your factory you can fix it once and move on.Legume
M
1

There are a lot of people who still like Test::Unit, and to some extent, it's a personal preference. However, on balance, you will find far more activity on the RSpec front. The really cool stuff is all being done with RSpec and Cucumber, so if you don't have a personal preference yourself, I'd probably skip Test::Unit. You should, however, be familiar enough with it to read someone else's tests that are written with it, but I wouldn't foresee that ever being a problem.

Morrissey answered 4/4, 2009 at 14:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.