Skip to content

RSpec unit testing

RSpec is the easiest tool to use to quickly test your Ruby code.

Output formats

RSpec supports the following outputs:

  • progress minimal output only showing a . for success and F for failure.
  • documentaion a text type output showing results as text.
  • RspecJunitFormatter a custom formatter outputting JUnit. <-- Supported by Calliope

Getting JUnit XML output

See RSpec JUnit Formatter.

Add the gem to your Gemfile,

gem 'rspec_junit_formatter'

or install it separately.

gem install rspec_junit_formatter

Then run your RSpec with the new formatter.

rspec --format RspecJunitFormatter --out rspec.xml

Adding it to your CI/CD

GitLab

stages:
- build
- test

# Build whatever project you are testing
build:
  stage: build
  script:
  - docker build --tag=eu.gcr.io/$GC_PROJECT/calliope:$CI_COMMIT_REF_SLUG-$REVISION .

# Test it referring to yhe build tag
test:
  stage: test
  variables:
    PROFILE_ID_TEST: 248
    PROFILE_ID_MASTER: 248
    PROFILE_ID_DEVELOPMENT: 248
    CALLIOPE_API_KEY: YourApiKey
  script:
  - mkdir output
  - docker run -e "RAILS_ENV=development" --rm \
    -v $PWD/output:/srv/output \
    --name testing_calliope_build eu.gcr.io/$GC_PROJECT/calliope:$CI_COMMIT_REF_SLUG-$REVISION \
    bundle exec rspec -f d -f RspecJunitFormatter --out /srv/output/rspec.xml --require rails_helper
  - if [ "$CI_COMMIT_REF_SLUG" == "master" ]; then CALLIOPE_PROFILE_ID="${PROFILE_ID_MASTER}"; fi
  - if [ "$CI_COMMIT_REF_SLUG" == "test" ]; then CALLIOPE_PROFILE_ID="${PROFILE_ID_TEST}"; else CALLIOPE_PROFILE_ID="${PROFILE_ID_DEVELOPMENT}"; fi
  - curl -X POST
         --header "Content-Type:text/xml"
         --header "x-api-key:${CALLIOPE_API_KEY}"
         --form "file[]=@output/rspec.xml"
         "https://app.calliope.pro/api/v2/profile/${CALLIOPE_PROFILE_ID}/report/import/junit?os=RSpec&platform=$CI_COMMIT_REF_SLUG&build=$REVISION"

Feedback or additions

If you have and feedback or wish to suggestion future additions to this documentation, please let us know.