# MiniTest With GitHub Actions

> In this short overview, see how MiniTest can be incorporated into your GitHub Actions workflows.

- Canonical: https://dennisokeeffe.com/blog/2022-03-18-minitest-with-github-actions
- Published: 2022-03-18
- Updated: 2022-03-18
- Tags: ruby, github-actions

The other post from today covered [GitHub Actions with RSpec](https://blog.dennisokeeffe.com/blog/2022-03-18-rspec-with-github-actions). 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](https://blog.dennisokeeffe.com/blog/2022-03-18-minitest-with-github-actions). For an alternative example that runs RSpec tests with code coverage for PRs, see my other post ["SimpleCov With Ruby And GitHub Actions"](https://blog.dennisokeeffe.com/blog/2022-03-12-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](https://github.com/okeeffed/minitest-github-actions).

## Prerequisites

1. Basic familiarity with [Bundler](https://bundler.io/).
2. Familiarity with reading tests written in [MiniTest](https://github.com/seattlerb/minitest).
3. Familiarity with [Rake](https://github.com/ruby/rake).

## Getting started

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

```s
$ 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](https://medium.com/@mario_chavez/testing-rails-with-minitest-7b4f99d4fcb8), 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:

```rb
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:

```rb
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:

```rb
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:

```s
$ 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`:

```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](https://cdn.dennisokeeffe.com/assets/2022-03-18-minitest-with-github-actions/1-test-running.webp)

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

![Successful job](https://cdn.dennisokeeffe.com/assets/2022-03-18-minitest-with-github-actions/2-test-completed.webp)

> 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

- [Bundler](https://bundler.io/)
- [MiniTest](https://github.com/seattlerb/minitest)
- [GitHub Actions](https://github.com/features/actions)
- [Source code](https://github.com/okeeffed/minitest-github-actions)
- ["SimpleCov With Ruby And GitHub Actions"](https://blog.dennisokeeffe.com/blog/2022-03-12-simplecov-with-ruby-and-github-actions)

_Photo credit: [`pawel_czerwinski`](https://unsplash.com/@pawel_czerwinski)_
