« Backpropagation in RubySignals & Slots in Ruby »

Unit Testing in Ruby

Motivation

Unit Testing is a natural way of maintaining a class library. I actually switched to writing the test cases before the actual implementation, or both simultaneously, quickly after writing the first test. If you don't believe me that unit testing is natural and inevitable, just do not do it and keep count of the problems you could have avoided in, say, a 5-year maintenance of some project involving more than two programmers.

Ruby=short

Allow me to quote "Whenever I find a new feature in ruby it tends to be drastically easier than I expected and it makes me happy" [glu.ttono.us] Well, I could not have said it any better. In Ruby, your test case looks like this

# this makes it a runnable test
require 'test/unit'
# sample test case for class Klass
require 'klass'
class TestOfKlass
  def test_some_name # must start with test_
     k = Klass.new('gin')
     assert(k.name=='gin')
  end
  def test_sanity # for demonstration
     assert_equal(1,1)
  end
  def test_bud # and another one
     assert(true) # true true ..
  end
end
You can run it directly or within the RDT by "run as Test::Unit"

More more more...

However, you'll typically want to have a large test suit for a project assuring you that "all works as expected" assuming the above was called 'test_case_1.rb' and you got 3 more you could write

require 'test/unit/testsuite'
require 'test_case_1'
require 'test_case_2'
require 'test_case_3'
require 'test_case_4'
And run it.
Loaded suite test_all
Started
...........................
Finished in 12.4 seconds.

24 tests, 119 assertions, 0 failures, 0 errors

Ok, I might rephrase "Whenever I want to do something reasonable in Ruby, it turns out to be drastically easy."

That's it, goodbye with a large thank-you to Nathaniel Talbott, author of Test::Unit

by axel
01/20/06. 11:20:55 am. 294 words, 3705 views. Categories: programming, Ruby , Leave a comment »Send a trackback »

Trackback address for this post

Trackback URL (right click and copy shortcut/link location)

Feedback awaiting moderation

This post has 1648 feedbacks awaiting moderation...

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
PoorExcellent
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)