MiniTest With GitHub Actions
Published: Mar 18, 2022
Last updated: Mar 18, 2022
The other post from today covered GitHub Actions with RSpec
If you are looking for the RSpec example, you can find it here
Source code can be found here
Prerequisites
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:
- Initialize the project with Bundler.
- Make some folders and files for our code, tests and GitHub workflows.
As noted in this blog post
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
Click into the action and let it run, and once there is a success you can see the output in the 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
Dennis O'Keeffe
Melbourne, Australia
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