How to add additional directories to test

Neeraj Singh

By Neeraj Singh

on April 26, 2014

In a project we needed to write different parsers for different services. Rather than putting all those parsers in app/models or in lib we created a new directory. We put all the parsers in app/parsers .

We put all the tests for these parsers in test/parsers directory.

We can run tests parsers individually by executing rake test test/parsers/email_parser_test.rb. However when we run rake then tests in test/parsers are not picked up.

We added following code to Rakefile to make rake pickup tests in test/parsers.

1
2# Adding test/parsers directory to rake test.
3namespace :test do
4  desc "Test tests/parsers/* code"
5  Rails::TestTask.new(parsers: 'test:prepare') do |t|
6    t.pattern = 'test/parsers/**/*_test.rb'
7  end
8end
9
10Rake::Task['test:run'].enhance ["test:parsers"]
11

Now when we run rake or rake test then tests under test/parsers are also picked up.

Above code adds a rake task rake test:parsers which would run all tests under test/parsers directory.

We can see this task by execute rake -T test.

1
2$ rake -T test
3rake test         # Runs test:units, test:functionals, test:integration together
4rake test:all     # Run tests quickly by merging all types and not resetting db
5rake test:all:db  # Run tests quickly, but also reset db
6rake test:parsers # Test tests/parsers/* code
7rake test:recent  # Run tests for {:recent=>["test:deprecated", "test:prepare"]} / Deprecated; Test recent changes
8

If this blog was helpful, check out our full blog archive.

Stay up to date with our blogs.

Subscribe to receive email notifications for new blog posts.