DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Pipeline as a Service: How To Test Pipelines in GitLab
  • Test Automation: Maven Profiles and Parallelization in Azure Pipelines Using IaaS
  • Testcontainers: From Zero To Hero [Video]
  • Mule 4 CI/CD Using GitLab Pipelines and JFrog Artifactory

Trending

  • ITBench, Part 1: Next-Gen Benchmarking for IT Automation Evaluation
  • Navigating and Modernizing Legacy Codebases: A Developer's Guide to AI-Assisted Code Understanding
  • The Role of AI in Identity and Access Management for Organizations
  • Agile’s Quarter-Century Crisis
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. DevOps and CI/CD
  4. How to Run GitLab CI Pipeline With Parallel RSpec Tests in Ruby

How to Run GitLab CI Pipeline With Parallel RSpec Tests in Ruby

Learn how to run parallel jobs for RSpec tests on GitLab CI Pipeline and speed up Ruby and JavaScript testing.

By 
Artur Trzop user avatar
Artur Trzop
·
Jan. 16, 21 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
9.2K Views

Join the DZone community and get the full member experience.

Join For Free

GitLab CI allows you to run tests much faster thanks to its CI parallelization feature. You can run parallel jobs across multiple GitLab Runners. In order to do it, you will learn how to split tests in a dynamic way across parallel tasks to ensure there is no bottleneck in GitLab Pipeline. Thanks to that CI build can be run as fast as possible so your Ruby and JS tests can be finely fast.

GitLab CI Parallelization

The common problem when you want to run tests in parallel to complete your one-hour test suite in a few minutes instead of waiting hours is to find a way to split tests on parallel jobs. Some of your Ruby or JavaScript tests can take milliseconds and some even a few minutes per test file (for instance, when using Capybara in RSpec features testing). The problem with slow tests also occurs in E2E (end to end testing) when using Cypress test runner as browser testing can take quite a long time to execute.

If you add more parallel GitLab Runners, you also may notice that some runners can start work later or not all jobs can be started at the same time (for instance, when you run GitLab Runners on your own infrastructure and other CI builds occupy some of the runners).

Dynamic Test Suite Split to Eliminate CI Build Bottlenecks

A solution to optimal tests distribution across parallel jobs (parallel CI nodes) is to distribute test files in smaller chunks across parallel GitLab runners. This ensures active runners can consume and execute your tests while too busy runners with slow tests would run fewer test cases. What is important is to ensure that all parallel runners will finish work at a similar time and thanks to that you won’t see stuck GitLab runner with too much work to process.

To split tests in a dynamic way for Ruby & JavaScript tests you can use Queue Mode in Knapsack Pro. Below, I will explain how Queue Mode works and what problems it solves.

GitLab YAML Config for Parallel Testing

Here, you can find an example config .gitlab-ci.yml for Ruby on Rails project that has RSpec tests executed across two parallel jobs using Knapsack Pro Queue Mode. A similar configuration would be for JavaScript projects with Jest or Cypress tests (here is a full list of supported test runners in Knapsack Pro).

Please remember to set API token for Knapsack Pro as environment variable name KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC in GitLab Settings -> CI/CD -> Variables (Expand) as a masked variable.

YAML
 




x
28


 
1
# .gitlab-ci.yml
2
image: "ruby:2.6"
3

          
4
services:
5
  - postgres
6

          
7
variables:
8
  RAILS_ENV: test
9
  POSTGRES_DB: database_name
10
  POSTGRES_USER: postgres
11
  POSTGRES_PASSWORD: ""
12
  DATABASE_URL: "postgresql://postgres:postgres@postgres:5432/database_name"
13
  # KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC: it is set in Settings -> CI/CD -> Variables (Expand) as masked variable
14

          
15
before_script:
16
  - apt-get update -qq && apt-get install -y -qq nodejs
17
  - ruby -v
18
  - which ruby
19
  - gem install bundler --no-document
20
  - bundle install --jobs $(nproc)  "${FLAGS[@]}"
21

          
22
  # Database setup
23
  - bin/rails db:setup
24

          
25
rspec:
26
  parallel: 2
27
  script:
28
    - bundle exec rake knapsack_pro:queue:rspec


Note: You can run dozens of parallel jobs by changing the parallel option and thanks to that run the very long test suite in a few minutes instead of waiting hour.

Summary

GitLab with its CI/CD tool allows you to run fast CI builds thanks to parallelization of your tests. By using Knapsack Pro Queue Mode you can ensure your tests are split across parallel jobs in an optimal way so your team gets test results as fast as possible.

If you are looking for an optimal CI solution for your project, check out our comparisons: GitLab CI vs. GitHub Actions, GitLab vs Circle CI, Jenkins vs. GitLab CI, or GitLab vs. other CI providers.

Continuous Integration/Deployment Testing GitLab Pipeline (software)

Published at DZone with permission of Artur Trzop. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Pipeline as a Service: How To Test Pipelines in GitLab
  • Test Automation: Maven Profiles and Parallelization in Azure Pipelines Using IaaS
  • Testcontainers: From Zero To Hero [Video]
  • Mule 4 CI/CD Using GitLab Pipelines and JFrog Artifactory

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!