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

2

20

In Java typically you would create two source folders src and test with an identical package hierarchy.

In Ruby do you just put all the tests in the same folder as the class under test? Or do you create a similar hierarchy in a separate folder? If so, how do you manage the require paths in your unit tests?

Pazpaza answered 20/11, 2011 at 15:26 Comment(2)
@nash: This is the first comment I read on SO that I found rude.Commonable
Sorry, it was really rude. I'll try to figure it out. For example, you can look at rspec spec folder here(github.com/rspec/rspec-core/tree/master/spec) These guys really know how to write specs. Sorry again.Ravenous
C
13

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.

Centra answered 20/11, 2011 at 18:36 Comment(2)
Thanks for the thorough answer! I notice there is often an spec_helper that contains dependencies in rspec projects.Pazpaza
To be clear, your gem will have \lib and \test directories.Poster
C
0

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.

Classieclassification answered 13/1, 2013 at 3:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.