Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
K

knapsack

> DevOps
Open source

Knapsack splits tests evenly across parallel CI nodes to run fast CI build and save you time.

550 stars0 likes1 views
WebsiteGitHub

About

Knapsack splits tests evenly across parallel CI nodes to run fast CI build and save you time.

[!WARNING] Knapsack is archived. But Knapsack Pro is available.

Knapsack Pro comes with a free plan for OSS projects and discounts on paid plans for people coming from Knapsack (see how to migrate in 10 minutes).

This repository remains available to fork and the gem hosted on RubyGems, so your existing setup won't be affected.

Speed up your tests

Run your 1-hour test suite in 2 minutes with optimal parallelisation on your existing CI infrastructure


Knapsack wraps your current test runner and works with your existing CI infrastructure to split tests optimally.

It comes in two flavors, knapsack and knapsack_pro:

knapsack knapsack_pro
Free ✅ ✅ OSS projects
Static split ✅ ✅
Dynamic split ❌ ✅
Split by test examples ❌ ✅
Graphs, metrics, and stats ❌ ✅
Programming languages (Ruby) ✅ (Ruby, Cypress, Jest, SDK/API)
CI providers Limited ✅ (All)
Heroku add-on ❌ ✅
Automated execution time recording ❌ ✅
Test split based on most recent execution times ❌ ✅
Support for spot/preemptible CI nodes ❌ ✅
Additional features ❌ (Overview)
Install Install

Migrate from knapsack to knapsack_pro

If you already use knapsack and want to give knapsack_pro a try, here's how to migrate in 10 minutes.

knapsack

Knapsack generates a test time execution report and uses it for future test runs.

The knapsack gem supports:

  • RSpec
  • Cucumber
  • Minitest
  • Spinach
  • Turnip

Without Knapsack - bad test suite split

With Knapsack - better test suite split

Requirements

>= Ruby 2.1.0


  • Update
  • Installation
  • Usage
    • Step for RSpec
    • Step for Cucumber
    • Step for Minitest
    • Step for Spinach
    • Custom configuration
    • Common step
      • Adding or removing tests
  • Set up your CI server
    • Info about ENV variables
    • Passing arguments to the Rake task
      • Passing arguments to RSpec
      • Passing arguments to Cucumber
      • Passing arguments to Minitest
      • Passing arguments to Spinach
    • Knapsack binary
    • CircleCI
      • Step 1
      • Step 2
    • Travis
      • Step 1
      • Step 2
    • Semaphore
      • Step 1
      • Step 2
        • Semaphore 2.0
    • Buildkite
      • Step 1
      • Step 2
    • GitLab CI
      • Step 1
      • Step 2
    • Info for Jenkins
    • Info for BitBucket Pipelines
      • Step 1
      • Step 2
  • FAQ
    • What does time offset warning mean?
    • How to generate the Knapsack report?
    • What does "leftover specs" mean?
    • Why are there "leftover specs" after I generate a new report?
    • How can I run tests from multiple directories?
    • How to update the existing Knapsack report for a few test files?
    • How to run tests for particular CI node in your development environment
    • How can I change the log level?
  • Gem tests
    • Spec
    • Spec examples
  • Acknowledgements
  • Mentions

Update

Please check CHANGELOG.md before updating the gem. Knapsack follows semantic versioning.

Installation

Add these lines to your application's Gemfile:

ruby
group :test, :development do
  gem 'knapsack'
end

And then execute:

bash
bundle

Add this line at the bottom of Rakefile:

ruby
Knapsack.load_tasks if defined?(Knapsack)

Usage

Here's an example of a Rails app with Knapsack.

https://github.com/KnapsackPro/rails-app-with-knapsack

Step for RSpec

Add at the beginning of your spec_helper.rb:

ruby
require 'knapsack'

# CUSTOM_CONFIG_GOES_HERE

Knapsack::Adapters::RSpecAdapter.bind

Step for Cucumber

Create features/support/knapsack.rb:

ruby
require 'knapsack'

# CUSTOM_CONFIG_GOES_HERE

Knapsack::Adapters::CucumberAdapter.bind

Step for Minitest

Add the Knapsack code after you load the app environment in test/test_helper.rb:

ruby
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

require 'knapsack'

# CUSTOM_CONFIG_GOES_HERE

knapsack_adapter = Knapsack::Adapters::MinitestAdapter.bind
knapsack_adapter.set_test_helper_path(__FILE__)

Step for Spinach

Create features/support/env.rb:

ruby
require 'knapsack'

# CUSTOM_CONFIG_GOES_HERE

Knapsack::Adapters::SpinachAdapter.bind

Custom configuration

You can change the default Knapsack configuration for RSpec, Cucumber, Minitest, or Spinach tests.

Here are some examples (that you can insert in CUSTOM_CONFIG_GOES_HERE):

ruby
Knapsack.tracker.config({
  enable_time_offset_warning: true,
  time_offset_in_seconds: 30
})

Knapsack.report.config({
  test_file_pattern: 'spec/**{,/*/**}/*_spec.rb', # default value based on adapter
  report_path: 'knapsack_custom_report.json'
})

# You can use your logger:
require 'logger'
Knapsack.logger = Logger.new(STDOUT)
Knapsack.logger.level = Logger::INFO

Common step

Generate the time execution report for your test files. Run the command below on one of your CI nodes:

bash
# Step for RSpec:
KNAPSACK_GENERATE_REPORT=true bundle exec rspec spec

# Step for Cucumber:
KNAPSACK_GENERATE_REPORT=true bundle exec cucumber features

# Step for Minitest:
KNAPSACK_GENERATE_REPORT=true bundle exec rake test

# If you use Rails 5.0.x then run this instead:
KNAPSACK_GENERATE_REPORT=true bundle exec rake test

# If you use Rails >= 5.1's SystemTest, run both unit and system tests:
KNAPSACK_GENERATE_REPORT=true bundle exec rake test test:system

# Step for Spinach:
KNAPSACK_GENERATE_REPORT=true bundle exec spinach

Commit the generated report knapsack_rspec_report.json, knapsack_cucumber_report.json, knapsack_minitest_report.json or knapsack_spinach_report.json into your repository.

This report should be updated after you add a lot of new slow tests or you change existing ones, which causes a big time execution difference between CI nodes.

You will get a time offset warning at the end of the RSpec/Cucumber/Minitest run, which reminds you when it’s a good time to regenerate the Knapsack report.

KNAPSACK_GENERATE_REPORT is truthy with "true" or 0. All other values are falsy, though "false" and 1 are semantically preferrable.

Adding or removing tests

There is no need to regenerate the report every time you add/remove test files.

If you remove a test file, Knapsack will ignore its entry in the report. If you add a new test file that is not listed in the report, the test file will be assigned to one of the CI nodes.

You'll want to regenerate your execution report whenever you remove or add a test file with a long time execution time that would affect one of the CI nodes. Knapsack warns you when it's a good time to regenerate the report.

Set up your CI server

On your CI server, run the following command for the first CI node (increase CI_NODE_INDEX for the next nodes):

bash
# Step for RSpec:
CI_NODE_TOTAL=2 CI_NODE_INDEX=0 bundle exec rake knapsack:rspec

# Step for Cucumber:
CI_NODE_TOTAL=2 CI_NODE_INDEX=0 bundle exec rake knapsack:cucumber

# Step for Minitest:
CI_NODE_TOTAL=2 CI_NODE_INDEX=0 bundle exec rake knapsack:minitest

# Step for Spinach:
CI_NODE_TOTAL=2 CI_NODE_INDEX=0 bundle exec rake knapsack:spinach

You can add KNAPSACK_TEST_FILE_PATTERN if your tests are not in the default directory:

…

You can set KNAPSACK_REPORT_PATH if your Knapsack report was saved in a non-default location:

bash
# Step for RSpec:
KNAPSACK_REPORT_PATH="knapsack_custom_report.json" CI_NODE_TOTAL=2 CI_NODE_INDEX=0 bundle exec rake knapsack:rspec

# Step for Cucumber:
KNAPSACK_REPORT_PATH="knapsack_custom_report.json" CI_NODE_TOTAL=2 CI_NODE_INDEX=0 bundle exec rake knapsack:cucumber

# Step for Minitest:
KNAPSACK_REPORT_PATH="knapsack_custom_report.json" CI_NODE_TOTAL=2 CI_NODE_INDEX=0 bundle exec rake knapsack:minitest

# Step for Spinach:
KNAPSACK_REPORT_PATH="knapsack_custom_report.json" CI_NODE_TOTAL=2 CI_NODE_INDEX=0 bundle exec rake knapsack:spinach

Info about ENV variables

CI_NODE_TOTAL - total number of CI nodes you have.

CI_NODE_INDEX - index of the current CI node starting from 0 (ie, the second CI node should have CI_NODE_INDEX=1).

Some CI providers like GitLab CI have the same name of environment variable like CI_NODE_INDEX, which starts from 1 instead of 0. Knapsack will automatically pick it up and change it from 1 to 0.

Passing arguments to the Rake task

Passing arguments to RSpec

Knapsack allows you to pass arguments through to RSpec. For example, if you want to run only specs that have the tag focus. If you do this with RSpec directly, it would look like:

bash
bundle exec rake rspec --tag focus

To do this with Knapsack, you simply add your RSpec arguments as parameters to the Knapsack Rake task:

bash
bundle exec rake "knapsack:rspec[--tag focus]"

Remember that using tags to limit which specs get run will affect the time each file takes to run. One solution to this is to generate a new knapsack_rspec_report.json for the commonly run scenarios.

Passing arguments to Cucumber

bash
bundle exec rake "knapsack:cucumber[--name feature]"

Passing arguments to Minitest

bash

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyauto-balancingbuildkiteci-servercircle-ci

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
CategoryDevOps
PricingOpen source

> Related tools

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理