Just require the filename without the .rb
extension like so:
require './test'
Suppose you have this directory structure:
+-- project/
| |
| +-- folder1/
| | |
| | +-- file1.rb
| |
| +-- folder2/
| |
| +-- file2.rb
|
+-- file3.rb
in this case you may want to add specific directories to the load path like so:
# in file3.rb
$LOAD_PATH.unshift './folder1'
this way you can require files by their name without specifying the folder every time:
require 'file1'
Now for the second part, getting an instance. You could just do AAA.new
but i suppose you want to dynamically create instances of classes that are subclasses of TestCase
. First you have to find all subclasses of TestCase
:
class Class
def subclasses
constants.map do |c|
const_get(c)
end.select do |c|
c.is_a? Class
end
end
end
this will enable you to get a list of subclasses like so:
TestCase.subclasses
#=> [TestCase::AAA]
from which you can construct your objects
TestCase.subclasses.map{|klass| klass.new }
#=> [#<TestCase::AAA:0x007fc8296b07f8>]
or even shorter if you do not need to pass arguments to new
TestCase.subclasses.map(&:new)
#=> [#<TestCase::AAA:0x007fc8296b07d0>]
So far, so good. But if i get this right you are trying to build a test runner. Don't. There are plenty of testing libraries and great test runners out there. Ruby has the built-in Minitest and this blog post explains very well how you can best run your tests.