Skip to content

Instantly share code, notes, and snippets.

@daveworth
Created December 18, 2011 00:49
Show Gist options
  • Select an option

  • Save daveworth/1491978 to your computer and use it in GitHub Desktop.

Select an option

Save daveworth/1491978 to your computer and use it in GitHub Desktop.
require 'test_helper'
class TestDynamic < Test::Unit::TestCase
def setup; end
def teardown; end
def test_method1; end
def test_method2; end
end
# This re-opening of the test-suite class is to add the "ugly"
# dynamic `#test_parse_*` methods
class TestDynamic
addrs = [
{:text => "test_data",
:result => "test data"},
{:text => "different data",
:result => "different data",
].each do |fixture|
define_method "test_parse_#{fixture[:text].tr('.,', '').gsub(/\s+/, '_')}" do
assert_equal fixture[:result], some_method(fixture[:text])
end
end
end
@daveworth
Copy link
Author

The punchline as to how this works is weird... I wanted to do something like

class TestDynamic < Test::Unit::TestCase
  def setup
    build_tests
  end

  def build_tests
    fixtures = [ ... ].each do |fixture|
      define_method "test_#{fixture_name}" do
        check_addr(fixture)
      end
    end
end

This does not work because #setup is only called before each test (kinda like before (:each); do for RSpec). Instead, relying on the fact that all code in a class is executed, works perfectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment