What is the best practice for organizing Ruby test folder structure? What is the best practice for organizing Ruby test folder structure? ruby ruby

What is the best practice for organizing Ruby test folder structure?


At first, each gem has a typical layout. Code is almost completely in lib. In the root directory, there is only metadata like the README, gemspec file and some optional configuration data. If you write a web app with something like Rails or Sinatra, their layout standards are used instead.

In all those project types, tests can be found in similar locations though. Depending on which testing framework you use, there are different standards.

If you use Test::Unit or minitest, tests usually are in a test directory. There are no real standards on how to actually organise the test files in that directory. I personally found it useful to at least partly mirror the file layout of the tested code. If you use modules/namespaces generously, that should make it rather readable when following this setup in your tests too.

If you use RSpec, the tests (then called specs) go into a spec directory. The above notes about the layout of the actual tests apply here too.

In the end, it's mostly your own decision how you set up your tests. As tests are an area where many people have different (and strong) opinions, there's no holy path to success. You should take a look at some gems you use and how they do stuff. An example of minitest layouts can be found in the Rails gems, e.g. for ActiveRecord. An example for RSpec tests is my rackstash library.


I'm new to Ruby too, and wondering the same question. The part I didn't get was how to organize them hierarchically to match a potentially hierarchical organization of components in the lib directory, and then run them all as a suite.

I haven't been Googling that long, but my findings are already thinner than expected. The most helpful thing I've found is this from the ruby wiki:

Test case classes can be gathered together into test suites which are Ruby files which require other test cases:

# File: ts_allTheTests.rb
require 'test/unit'
require 'testOne'
require 'testTwo'
require 'testThree'

In this way, related test cases can be naturally grouped. Further, test suites can contain other test suites, allowing the construction of a hierarchy of tests.

Previously, I had been avoiding subdirectories in my test directory and doing something like this in my Rakefile, or any ruby file that actually executes the tests:

$LOAD_PATH << File.dirname(__FILE__)require 'test/unit'Dir.glob('test/test_*', &method(:require))

So if I combine the two techniques, I would have a file for each directory that dynamically requires tests from that directory, which in turn would be required by the file for the parent directory. But this seems to defeat my original effort to avoid tedium.

Then I found some classes in ruby-doc that sounded relevant but under-documented. However, it looks like there's more info available upward for Test::Unit that I could have easily missed. I haven't read it all yet, but it looks promising.