Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestCase clone issue when a class method is defined #210

Closed
dmarchaland opened this issue Feb 27, 2022 · 4 comments
Closed

TestCase clone issue when a class method is defined #210

dmarchaland opened this issue Feb 27, 2022 · 4 comments

Comments

@dmarchaland
Copy link

dmarchaland commented Feb 27, 2022

Hi,

I'm hitting the following issue when trying to clone a simple TestCase as defined below

class MyTest < Test::Unit::TestCase

  class << self
    def startup
    end
  end

  def test_trial
  end
  
 end
 
 MyTest.clone
test-unit-3.5.3/lib/test/unit/attribute.rb:203:in \`find_attribute': undefined method \`find_attribute' for nil:NilClass (NoMethodError)
	from test-unit-3.5.3/lib/test/unit/testcase.rb:151:in \`method_added'

This is using Ruby 2.6.8

@kou kou changed the title TestCase clown issue when a class method is defined TestCase clone issue when a class method is defined Feb 27, 2022
@kou kou closed this as completed in 3058e8a Feb 27, 2022
@kou
Copy link
Member

kou commented Feb 27, 2022

I've fixed this but why do you want to clone?

@dmarchaland
Copy link
Author

dmarchaland commented Feb 27, 2022

I wrote a generic TestCase that I want to run on several objects dynamically generated. See pseudo code below for the idea. Basic question is how to reuse a TestCase multiple times?

class MyGenericTestCase < Test::Unit::TestCase

  class << self

    attr_accessor :obj

    def startup
    end

  end

  def test_obj
    assert(self.class.obj)
  end

end


ListOfObjs = [['A'], ['B'], ['C']]

MyTestSuite = Test::Unit::TestSuite.new("My TestSuite")

ListOfObjs.each do |obj|
  klassname = "TestCase_#{obj.first}"
  klass = Object.const_set(klassname, MyGenericTestCase.clone)
  klass.obj = obj
  MyTestSuite << klass.suite
end

@kou
Copy link
Member

kou commented Feb 28, 2022

You should use module for shared tests and inheritance for the parameterization like normal Ruby script:

require "test-unit"

module MyGenericTests
  def test_obj
    assert(self.class.obj)
  end
end

class MyGenericTestCase < Test::Unit::TestCase

  class << self

    attr_accessor :obj

    def startup
    end
  end
end


ListOfObjs = [['A'], ['B'], ['C']]

ListOfObjs.each do |obj|
  klassname = "TestCase_#{obj.first}"
  klass = Object.const_set(klassname, Class.new(MyGenericTestCase))
  klass.obj = obj
  klass.include(MyGenericTests)
end

Or you can use data-driven test feature:

require "test-unit"

class MyGenericTestCase < Test::Unit::TestCase
  class << self
    def startup
    end
  end

  data(:obj, ["A", "B", "C"], keep: true)
  def test_obj1(data)
    obj = data[:obj]
    assert(obj)
  end

  def test_obj2(data)
    obj = data[:obj]
    assert(obj)
  end
end

@dmarchaland
Copy link
Author

Module usage makes definitely sense to define the generic tests! Thank you, appreciated your input.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants