Nice new edge feature: test/do declaration style testing

Posted by Jonathan

Rails 2 introduced ActiveSupport::TestCase and friends, RoR's enhancement of Test::Unit.

Those extra classes made testing Rails controllers easier and removed the need for cluttered setup methods. Today DHH committed a new feature to ActiveSupport::TestCase (by Jay Fields) that allows Rails tests to match up with RSpec's and Shoulda's nicer declaration style test naming: test/do declaration style testing.

In plain Test::Unit each test would be a method named 'test_' followed by the name the test:

def test_email_format_is_validated
  ...
end

def test_invalid_credit_card_number_throws_exception
  ...
end

This works ok but is a bit clumsy and gets ugly with long method names. In edge you can now write the test like this

test 'email format is validated' do
  ...
end

test 'invalid credit card number throws exception' do
  ...
end

What happens in the background is that ActiveSupport::TestCase will just generate the test_email_format_is_validated method for you. What is missing is a nice integration with the test runner.

This brings Rails developers that envy RSpec's and Shoulda's declarative style to the same level. RSpec&co can still do more tricks but most developers I know really just lust for the it 'should do as I want it to' do ... end syntax and don't really care about the a.should == b.

Assert valid UTF-8 test helper

Posted by Jonathan

A small test helper that can be useful:

def assert_valid_utf_8(text)
  assert_nothing_raised{
    text.each_char{|char| char.unpack('U') }
  }
end

Test Frameworks NG

Posted by Jonathan

I’ve stumbled over a nice comparison of test frameworks for Java on TheServerSide.com. It compares JUnit 3.x, JTiger, and TestNG. Further it has a look at the next version of JUnit that will use new features of JDK 1.5. Also Frank Westphal has a good article on JUnit 4 (german, but the code is self-explanatory).

What is apparent from these articles is that the new trend for test frameworks is to disband the habit of forcing naming conventions like setUp(), tearDown(), testXXX() and ClassTest. Instead JDK 1.5 annotations are used:

public class ClassTest {
 @before public void init() {
  ...
  ...
 }
 @Test public void emptyList() {
  ...
  ...
 }
 @after public void clear() {
  ...
  ...
 }
}

Further the various assert* methods are expanded and more complex tests are possible. Make sure to read the articles if you are interested in Unit Testing even if you have no interest in Java as these features are interesting for every Unit Test framework.

Currently Ruby’s Test::Unit is built in style of JUnit 3.x. Are there any efforts to create a JUnit 4.x style Unit Test framework for Ruby?