Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

Duplicate test name detection

Ruby does not throw any exceptions or warnings if an object defines two methods with the same name. The second definition always wins. While this provides great flexibility for many Ruby tasks it can be problematic when writing tests. In particular, if you define two tests with same name in the same test class, one will get run and the other will not. If you have been writing tests for a while you understand that there is nothing worse than writing a test that never gets used!

Recently Matthew O’Connor and I set out to fix this problem for Test::Unit by alias method chaining :method_added for TestCase classes and their subclasses. We use the inherited hook on Ruby classes to dynamically define a method_added hook for every test case. This is required because method_added does not get inherited between classes, so it can’t be defined only in Test::Unit::TestCase.

The following patch raises an exception if it detects a duplicated test name.

class Test::Unit::TestCase
  class << self
    def known_test_methods
      @known_test_methods ||=

    def record_test_method(method)
      if method.to_s.starts_with?("test")
        if known_test_methods.include? method
          raise "Duplicate test #{self}##{method}"
          known_test_methods << method

    def inherited(subclass)
      class << subclass
        def method_added_with_duplicate_check(method)
        alias_method_chain :method_added, :duplicate_check unless method_defined?(:method_added_without_duplicate_check)

When we first tried this against a legacy code base we found almost a dozen duplicated tests that were not running.

  1. Josh Susser says:

    Very useful check! But where did you hear that `method_added` wasn’t inherited? I’ve used it before and just double-checked again right now, and it inherits just fine. I also don’t see the need for doing an alias method chain. Just override method_added in TestCase and call super at the end.

    class Test::Unit::TestCase
    class << self def known_test_methods @known_test_methods ||= end def method_added(method) if method.to_s.starts_with?(“test”) if known_test_methods.include?(method) raise “Duplicate test #{self}##{method}” else known_test_methods << method end end super end end end

  2. Adam Milligan says:

    This is great stuff; the sort of thing that testing frameworks should have built in.

  3. Pat Nakajima says:

    Great check! I added a similar hook to my [fixjour]( project to make sure people don’t define redundant builder methods: [](

  4. grosser says:

    just switch to rspec/should/anything better then pure test and do yourself+everybody reading your code a favor + no need to worry about such wacky details

  5. Chris Heisterkamp says:


    You’re absolutely right about method_added inheriting in Ruby. We initially tried your method but something in our code base (Ruby 1.8.6 / Rails 2.0.2) seems to break/override this inheritance for test cases.

    The alias method chain was added because we didn’t want to make assumptions about whether or not these classes might already have a method_added, potentially blowing them away.

  6. Chris Heisterkamp says:


    We would love to use alternate frameworks, but we’re working with a large legacy code base and it’s not worth the time right now to introduce a new testing framework.

  7. Ryan Sobol says:

    Great piece of engineering work!

  8. Ryan Davis says:

    ruby -w?
    $-w = true?

Post a Comment

Your Information (Name required. Email address will not be displayed with comment.)

* Copy This Password *

* Type Or Paste Password Here *