Skip to main content

Using Knapsack Pro with CircleCI

Collect metadata in Queue Mode

You may want to collect metadata in CircleCI. For example, to collect junit reports of your RSpec runs you would:

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/tmp_rspec_#{ENV['KNAPSACK_PRO_CI_NODE_INDEX']}.xml"
FINAL_REPORT = "tmp/final_rspec_#{ENV['KNAPSACK_PRO_CI_NODE_INDEX']}.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

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

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.


.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

The snippet above: