Skip to main content

Using Knapsack Pro with CircleCI

Collect metadata in Queue Mode

You may want to collect metadata in CircleCI.

RSpec

To collect JUnit reports of your RSpec runs, you would:

.circleci/config.yml
- run:
name: RSpec with Knapsack Pro in Queue Mode
command: |
mkdir -p /tmp/test-results
bundle exec rake "knapsack_pro:queue:rspec[--format documentation --format RspecJunitFormatter --out /tmp/test-results/rspec.xml]"

- store_test_results:
path: /tmp/test-results

- store_artifacts:
path: /tmp/test-results
destination: test-results

You can find a complete CircleCI configuration in RSpec testing parallel jobs with CircleCI and JUnit XML report.

For legacy versions of knapsack_pro older than 7.0, please click here.
spec_helper.rb or rails_helper.rb
# `TMP_REPORT` must be the same path as `--out`
# `TMP_REPORT` must be a full path (no `~`)
TMP_REPORT = "tmp/rspec.xml"
FINAL_REPORT = "tmp/final_rspec.xml"

KnapsackPro::Hooks::Queue.after_subset_queue do |queue_id, subset_queue_id|
if File.exist?(TMP_REPORT)
FileUtils.mv(TMP_REPORT, FINAL_REPORT)
end
end

KnapsackPro::Hooks::Queue.after_queue do |queue_id|
if File.exist?(FINAL_REPORT) && ENV['CIRCLE_TEST_REPORTS']
FileUtils.cp(FINAL_REPORT, "#{ENV['CIRCLE_TEST_REPORTS']}/rspec.xml")
end
end

Cucumber

Please provide a directory path in the --out option, where JUnit XML reports are generated. The directory path tmp/test-reports/ must end with / as shown in the following example:

.circleci/config.yml
- run:
name: Cucumber with Knapsack Pro in Queue Mode
command: |
mkdir -p /tmp/test-results
bundle exec rake "knapsack_pro:queue:cucumber[--format junit --out tmp/test-reports/]"

- store_test_results:
path: /tmp/test-results

- store_artifacts:
path: /tmp/test-results
destination: test-results

The XML report is generated for each batch of tests fetched from the Queue API. Multiple reports are created on each CI node in Queue Mode.

Rerun only failed tests

Use the CircleCI rerun failed tests feature with Knapsack Pro to only rerun a subset of tests instead of rerunning the entire test suite when a transient test failure arises.


caution

Remove KNAPSACK_PRO_FIXED_QUEUE_SPLIT if you use it, then the default false value is used, which allows rerunning failed tests correctly.


RSpec

.circleci/config.yml
- run:
name: RSpec with Knapsack Pro in Queue Mode
command: |
mkdir -p /tmp/test-results

export KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES=true

export KNAPSACK_PRO_TEST_FILE_LIST_SOURCE_FILE=/tmp/tests_to_run.txt
# Retrieve the tests to run (all or just the failed ones), and let Knapsack Pro split them optimally.
circleci tests glob "spec/**/*_spec.rb" | circleci tests run --index 0 --total 1 --command ">$KNAPSACK_PRO_TEST_FILE_LIST_SOURCE_FILE xargs -n1 echo" --verbose
bundle exec rake "knapsack_pro:queue:rspec[--format documentation --format RspecJunitFormatter --out /tmp/test-results/rspec.xml]"

- store_test_results:
path: /tmp/test-results

- store_artifacts:
path: /tmp/test-results
destination: test-results

The snippet above:

For legacy versions of knapsack_pro older than 7.0, please click here.
.circleci/config.yml
- run:
name: RSpec with Knapsack Pro in Queue Mode
command: |
export CIRCLE_TEST_REPORTS=/tmp/test-results
mkdir -p $CIRCLE_TEST_REPORTS

export KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES=true

export KNAPSACK_PRO_TEST_FILE_LIST_SOURCE_FILE=/tmp/tests_to_run.txt
# Retrieve the tests to run (all or just the failed ones), and let Knapsack Pro split them optimally.
circleci tests glob "spec/**/*_spec.rb" | circleci tests run --index 0 --total 1 --command ">$KNAPSACK_PRO_TEST_FILE_LIST_SOURCE_FILE xargs -n1 echo" --verbose
bundle exec rake "knapsack_pro:queue:rspec[--format documentation --format RspecJunitFormatter --out tmp/rspec.xml]"
- store_test_results:
path: /tmp/test-results
- store_artifacts:
path: /tmp/test-results
destination: test-results

Minitest

Install the minitest-ci gem:

bundle add minitest-ci

Configure CircleCI:

.circleci/config.yml
- run:
name: Minitest with Knapsack Pro in Queue Mode
command: |
export KNAPSACK_PRO_TEST_FILE_LIST_SOURCE_FILE=/tmp/tests_to_run.txt

# Retrieve the tests to run (all or just the failed ones), and let Knapsack Pro split them optimally.
circleci tests glob "test/**/*_test.rb" | circleci tests run --index 0 --total 1 --command ">$KNAPSACK_PRO_TEST_FILE_LIST_SOURCE_FILE xargs -n1 echo" --verbose
bundle exec rake "knapsack_pro:queue:minitest[--verbose --ci-report --no-ci-clean]"

- store_test_results:
path: test/reports