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

Back to home

RSpec With GitHub Actions

GitHub Actions is a great way to run your RSpec tests in your CI/CD workflows.

This post covers how to setup RSpec testing when a push is made to a remote GitHub repo.

For an alternative example that runs RSpec tests with code coverage for PRs, see my other post "SimpleCov With Ruby And GitHub Actions".

Source code can be found here.


  1. Basic familiarity with Bundler.
  2. Familiarity with reading tests written in RSpec.

Getting started

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

$ mkdir rspec-github-actions $ cd rspec-github-actions # initialise Bundler project $ bundle init # RSpec added for testing $ bundle add rspec --group "development,test" # Add other required files $ mkdir lib spec $ touch lib/contrived_math.rb spec/math_spec.rb # Add the GitHub workflow folder $ mkdir -p .github/workflows $ touch .github/workflows/rspec.yml

In the above, we do the following:

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

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 spec/math_spec.rb, add the following code:

require 'contrived_math' RSpec.describe Math do describe '#add' do it 'adds two numbers' do expect(ContrivedMath.add(1, 2)).to eq(3) end end describe '#subtract' do it 'subtracts two numbers' do expect(ContrivedMath.subtract(2, 1)).to eq(1) end end end

At this point we can run our tests with bundle exec rspec and see our tests are passing.

Writing the GitHub Action

Inside of .github/workflows/rspec.yml:

name: Run RSpec tests on: [push] jobs: run-rspec-tests: 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: | bundle exec rspec

The above YAML configuration will tell GitHub to run an action on each push to remote where our job run-rspec-tests will outline the steps required to run the RSpec 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 running:

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 ran the job a second time (which is displayed in the screenshot above). The images have job #1 and #2 respectively but the result is the same.


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

Resources and further reading

Photo credit: pawel_czerwinski

Personal image

Dennis O'Keeffe

  • Melbourne, Australia

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


Get fresh posts + news direct to your inbox.

No spam. We only send you relevant content.

RSpec With GitHub Actions


Share this post