Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 695
Garrett Hall Avatar asked Nov 20 '11 15:11

Garrett Hall


1 Answers

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.

like image 63
Holger Just Avatar answered Sep 18 '22 07:09

Holger Just