Created
December 17, 2023 03:59
-
-
Save cmolina/a50f54848529a55965fb2d0eec64a1fe to your computer and use it in GitHub Desktop.
Sharding for Rails system tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :test do | |
namespace :system do | |
# Run `rails test:system` split into N shards | |
# | |
# @example run the first fifth of the tests | |
# $ rake test:system:shard\['1/5'\] | |
desc 'Run system tests in shards' | |
task :shard, [:shard] => [:environment] do |_, args| | |
tests = Rake::FileList['test/system/**/*_test.rb'] | |
raise ArgumentError, "Shard is missing, it should be in the format 'x/y'" if args[:shard].nil? | |
begin | |
shard_index, shards_count = args[:shard].split('/').map(&:to_f) | |
shards = tests.each_slice((tests.size / shards_count).ceil).to_a | |
rescue StandardError | |
raise ArgumentError, "Shard has wrong format, received '#{args[:shard]}', expected 'x/y'" | |
end | |
filenames = shards[shard_index - 1] || [] | |
system("bin/rails test #{filenames.join(' ')}", exception: true) unless filenames.empty? | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment