🎉 I'm releasing 12 products in 12 months! If you love product, checkout my new blog workingoutloud.dev

Back to home

MiniTest With GitHub Actions

The other post from today covered GitHub Actions with RSpec. This post will cover the equivalent with MiniTest tests in your CI/CD workflows.

If you are looking for the RSpec example, you can find it here. For an alternative example that runs RSpec tests with code coverage for PRs, see my other post "SimpleCov With Ruby And GitHub Actions" - you could adjust the GitHub workflow for MiniTest as demonstrated in this post.

Source code can be found here.

Prerequisites

  1. Basic familiarity with Bundler.
  2. Familiarity with reading tests written in MiniTest.
  3. Familiarity with Rake.

Getting started

We will create the project directory minitest-github-actions and use Bundler to initialize the project:

$ mkdir minitest-github-actions $ cd minitest-github-actions # initialise Bundler project $ bundle init # Add other required files $ mkdir lib test $ touch lib/contrived_math.rb test/test_contrived_math.rb Rakefile # Add the GitHub workflow folder $ mkdir -p .github/workflows $ touch .github/workflows/minitest.yml

In the above, we do the following:

  1. Initialize the project with Bundler.
  2. Make some folders and files for our code, tests and GitHub workflows.

As noted in this blog post, MiniTest ships with never versions of Ruby, so there may be no need for any other installation.

In our example, we are creating some contrived math functions to test against.

Writing our math code

Inside of lib/contrived_math.rb, add the following:

module ContrivedMath def self.add(a, b) a + b end def self.subtract(a, b) a - b end end

The above adds (unnecessary) functions for both addition and subtraction.

These will be the functions that we write tests for.

Our tests

Inside of test/test_contrived_math.rb, add the following code:

require 'minitest/autorun' require 'contrived_math' describe Math do describe '#add' do it 'adds two numbers' do _(ContrivedMath.add(1, 2)).must_equal 3 end end describe '#subtract' do it 'subtracts two numbers' do _(ContrivedMath.subtract(2, 1)).must_equal 1 end end end

Updating the Rakefile

We want to add a Rake task to make it easier to run our tests.

Inside of Rakefile, add the following:

require 'rake/testtask' Rake::TestTask.new do |t| t.libs << 'test' t.test_files = FileList['test/test*.rb'] t.verbose = true end desc 'Run tests' task default: :test

At this point, we could run rake test and we will get something along the lines of the following output:

$ rake test /path/to/ruby -w -I"lib:test" /path/to/rake/rake_test_loader.rb "test/test_contrived_math.rb" Run options: --seed 42452 # Running: .. Finished in 0.006387s, 313.1361 runs/s, 313.1361 assertions/s. 2 runs, 2 assertions, 0 failures, 0 errors, 0 skips

Awesome! All that is left is to create our workflow.

Writing the GitHub Action

Inside of .github/workflows/minitest.yml:

name: Run MiniTest on: [push] jobs: run-minitest: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Ruby uses: ruby/setup-ruby@v1 with: # Not needed with a .ruby-version file ruby-version: 2.7 # runs 'bundle install' and caches installed gems automatically bundler-cache: true - name: Run tests run: | rake test

The above YAML configuration will tell GitHub to run an action on each push to remote where our job run-minitest will outline the steps required to run the MiniTest tests.

Testing our action

At this point, all we need to do is push to the remote repo and see everything in action.

Note: I am expecting that you have initialized your Git repo at this point, then add all the files and set up remote.

Once you push your code to remote, checkout the Git repo and under the actions tab you will see your job queued (or running/completed):

Actions tab

Actions tab

Click into the action and let it run, and once there is a success you can see the output in the job.

Successful job

Successful job

Note: I had a few attempts to correct the flow thanks to lockfile issues, hence the three workflow runs.

Summary

Today's post demonstrated how to implement GitHub Actions with your MiniTest tests.

Resources and further reading

Photo credit: pawel_czerwinski

Personal image

Dennis O'Keeffe

@dennisokeeffe92
  • Melbourne, Australia

Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
I am currently working on Visibuild.

1,200+ PEOPLE ALREADY JOINED ❤️️

Get fresh posts + news direct to your inbox.

No spam. We only send you relevant content.

MiniTest With GitHub Actions

Introduction

Share this post