Search code examples
rubyunit-testingmakefilerake

Makefile for Ruby tests


I know about Rake::TestTask.

But, how would I write a Makefile to achive similar functionality?

I want to be able to run all my Ruby tests by running:

make test

It'd also be nice to have a way to run one test file, e.g.:

TEST=just_one_file.rb make test

I use MiniTest.

I want this because I like Make, and I want to start using it more.

I figured seeing this example would help me.

I also don't understand what rake test does under the scenes, so seeing the Makefile might help me understand how tests run.


Solution

  • Directory Structure

    ├── Makefile
    ├── app
    │   ├── controllers
    │   ├── helpers
    │   ├── views
    ├── test
    │   ├── controllers
    │   ├── helpers
    │   ├── test_helper.rb
    

    Makefile

    TEST := test/**/*_test.rb
    
    .PHONY : test
    
    test :
        ruby -Itest -e 'ARGV.each { |f| require "./#{f}" }' $(TEST)
    

    See how to run all the tests with minitest?

    Test Commands

    make test # runs all tests
    make test TEST=test/controllers/* # runs all tests in test/controllers
    make test TEST='test/controllers/users_controller_test.rb test/controllers/groups_controller_test.rb' # runs some tests
    make test TEST=test/controllers/users_controller_test.rb # runs a single test