Best Parallel_tests_ruby code snippet using ParallelTests.all
parallel_testing.rake
Source:parallel_testing.rake
...25# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.26#27# See doc/COPYRIGHT.rdoc for more details.28#++29namespace :parallel do30 desc 'Run all suites in parallel (one after another)'31 task all: ['parallel:plugins:spec', 'parallel:plugins:cucumber', :spec_legacy, :rspec, :cucumber]32 namespace :plugins do33 desc 'Run all plugin tests in parallel'34 task all: ['parallel:plugins:spec', 'parallel:plugins:cucumber']35 desc 'Run plugin specs in parallel'36 task spec: [:environment] do37 ParallelTests::Tasks.check_for_pending_migrations38 num_cpus = ENV['GROUP_SIZE']39 group = ENV['GROUP']40 group_options = num_cpus ? "-n #{num_cpus}" : ''41 group_options += " --only-group #{group}" if group42 spec_folders = Plugins::LoadPathHelper.spec_load_paths.join(' ')43 # Change this if changed in spec/support/rspec_failures.rb44 if File.exist? 'tmp/rspec-examples.txt'45 sh 'rm tmp/rspec-examples.txt'46 end47 cmd = "bundle exec parallel_test --type rspec #{group_options} #{spec_folders}"48 cmd += " || bundle exec rspec --only-failures #{spec_folders}"49 sh cmd50 end51 desc 'Run plugin cucumber features in parallel'52 task cucumber: [:environment] do53 ParallelTests::Tasks.check_for_pending_migrations54 num_cpus = ENV['GROUP_SIZE']55 group = ENV['GROUP']56 group_options = num_cpus ? "-n #{num_cpus}" : ''57 group_options += " --only-group #{group}" if group58 support_files = [Rails.root.join('features').to_s] + Plugins::LoadPathHelper.cucumber_load_paths59 support_files = support_files.map { |path|60 ['-r', Shellwords.escape(path)]61 }.flatten.join(' ')62 feature_folders = Plugins::LoadPathHelper.cucumber_load_paths.join(' ')63 cucumber_options = "-o ' -p rerun #{support_files}'"64 cmd = "bundle exec parallel_test --type cucumber #{cucumber_options} #{group_options} #{feature_folders}"65 cmd += " || bundle exec cucumber -p rerun #{support_files}"66 if File.exist? 'tmp/cucumber-rerun.txt'67 sh 'rm tmp/cucumber-rerun.txt'68 end69 sh cmd70 end71 end72 desc 'Run legacy specs in parallel'73 task :spec_legacy do74 ParallelTests::Tasks.check_for_pending_migrations75 num_cpus = ENV['GROUP_SIZE']76 group = ENV['GROUP']77 group_options = num_cpus ? "-n #{num_cpus}" : ''78 group_options += " --only-group #{group}" if group79 cmd = "bundle exec parallel_test --type rspec -o '-I spec_legacy' #{group_options} spec_legacy"80 cmd += ' || bundle exec rspec -I spec_legacy --only-failures spec_legacy'81 sh cmd82 end83 desc 'Run cucumber features in parallel (custom task)'84 task :cucumber do85 ParallelTests::Tasks.check_for_pending_migrations86 num_cpus = ENV['GROUP_SIZE']87 group = ENV['GROUP']88 group_options = num_cpus ? "-n #{num_cpus}" : ''89 group_options += " --only-group #{group}" if group90 support_files = [Rails.root.join('features').to_s] + Plugins::LoadPathHelper.cucumber_load_paths91 support_files = support_files.map { |path|92 ['-r', Shellwords.escape(path)]93 }.flatten.join(' ')94 cucumber_options = "-o ' -p rerun #{support_files}'"95 cmd = "bundle exec parallel_test --type cucumber #{cucumber_options} #{group_options} features"96 cmd += " || bundle exec cucumber -p rerun #{support_files}"97 if File.exist? 'tmp/cucumber-rerun.txt'98 sh 'rm tmp/cucumber-rerun.txt'99 end100 sh cmd101 end102 desc 'Run rspec in parallel (custom task)'103 task :rspec do104 ParallelTests::Tasks.check_for_pending_migrations105 num_cpus = ENV['GROUP_SIZE']106 group = ENV['GROUP']107 group_options = num_cpus ? "-n #{num_cpus}" : ''108 group_options += " --only-group #{group}" if group109 cmd = "bundle exec parallel_test --type rspec #{group_options} spec"110 cmd += ' || bundle exec rspec --only-failures'111 sh cmd112 end113end...
parallel_tests_spec.rb
Source:parallel_tests_spec.rb
1require File.dirname(__FILE__) + '/spec_helper'2describe ParallelTests do3 def size_of(group)4 group.inject(0) { |sum, test| sum += File.stat(test).size }5 end6 7 describe :tests_in_groups_of do8 before :all do9 system "rm -rf #{FAKE_RAILS_ROOT}; mkdir -p #{FAKE_RAILS_ROOT}/test/temp"10 1.upto(100) do |i|11 size = 100 * i12 File.open("#{FAKE_RAILS_ROOT}/test/temp/x#{i}_test.rb", 'w') { |f| f.puts 'x' * size }13 end14 end15 it "finds all tests" do16 found = ParallelTests.tests_in_groups(FAKE_RAILS_ROOT, 1)17 all = [ Dir["#{FAKE_RAILS_ROOT}/test/**/*_test.rb"] ]18 (found.flatten - all.flatten).should == []19 end20 it "partitions them into groups by equal size" do21 groups = ParallelTests.tests_in_groups(FAKE_RAILS_ROOT, 2)22 groups.size.should == 223 group0 = size_of(groups[0])24 group1 = size_of(groups[1])25 diff = group0 * 0.126 group0.should be_close(group1, diff)27 end28 29 it 'should partition correctly with a group size of 4' do30 groups = ParallelTests.tests_in_groups(FAKE_RAILS_ROOT, 4)31 groups.size.should == 432 group_size = size_of(groups[0])33 diff = group_size * 0.1 34 group_size.should be_close(size_of(groups[1]), diff)35 group_size.should be_close(size_of(groups[2]), diff)36 group_size.should be_close(size_of(groups[3]), diff)37 end38 it 'should partition correctly with an uneven group size' do39 groups = ParallelTests.tests_in_groups(FAKE_RAILS_ROOT, 3)40 groups.size.should == 341 group_size = size_of(groups[0])42 diff = group_size * 0.143 group_size.should be_close(size_of(groups[1]), diff)44 group_size.should be_close(size_of(groups[2]), diff)45 end46 end47 describe :run_tests do48 it "uses TEST_ENV_NUMBER=blank when called for process 0" do49 ParallelTests.should_receive(:open).with{|x|x=~/TEST_ENV_NUMBER= /}.and_return mock(:gets=>false)50 ParallelTests.run_tests(['xxx'],0)51 end52 it "uses TEST_ENV_NUMBER=2 when called for process 1" do53 ParallelTests.should_receive(:open).with{|x| x=~/TEST_ENV_NUMBER=2/}.and_return mock(:gets=>false)54 ParallelTests.run_tests(['xxx'],1)55 end56 it "returns the output" do57 io = open('spec/spec_helper.rb')58 ParallelTests.stub!(:print)59 ParallelTests.should_receive(:open).and_return io60 ParallelTests.run_tests(['xxx'],1).should =~ /\$LOAD_PATH << File/61 end62 end63 describe :find_results do64 it "finds multiple results in test output" do65 output = <<EOF66....F...67..68failute fsddsfsd69...70ff.**..710 examples, 0 failures, 0 pending72ff.**..731 example, 1 failure, 1 pending74EOF75 ParallelTests.find_results(output).should == ['0 examples, 0 failures, 0 pending','1 example, 1 failure, 1 pending']76 end77 it "is robust against scrambeled output" do78 output = <<EOF79....F...80..81failute fsddsfsd82...83ff.**..840 exFampl*es, 0 failures, 0 pend.ing85ff.**..861 exampF.les, 1 failures, 1 pend.ing87EOF88 ParallelTests.find_results(output).should == ['0 examples, 0 failures, 0 pending','1 examples, 1 failures, 1 pending']89 end90 end91 describe :failed do92 it "fails with single failed tests" do93 ParallelTests.failed?(['0 examples, 0 failures, 0 pending','1 examples, 1 failure, 1 pending']).should == true94 end95 it "fails with multiple failed tests" do96 ParallelTests.failed?(['0 examples, 1 failure, 0 pending','1 examples, 111 failures, 1 pending']).should == true97 end98 it "does not fail with successful tests" do99 ParallelTests.failed?(['0 examples, 0 failures, 0 pending','1 examples, 0 failures, 1 pending']).should == false100 end101 end102end...
ds.rake
Source:ds.rake
...17 task drop: :configure do18 @migrator.drop_keyspace19 end20 desc 'Migrate keyspace to latest version -- pass in model name ' \21 'to force an upload of just that one (all force-uploads everything).'22 task :migrate, [:force_cf] => :configure do |_t, args|23 if args[:force_cf].blank?24 @migrator.migrate_all25 else26 args[:force_cf] == 'all' ? @migrator.migrate_all(true) : @migrator.migrate_one(args[:force_cf].constantize, true)27 end28 end29 desc 'Alias for ds:migrate to maintain backwards-compatibility'30 task :schema, [:force_cf] => :migrate31 desc 'Rebuild SOLR Index -- pass in a model name (all rebuilds everything)'32 task :reindex, [:model] => :configure do |_t, args|33 if args[:model].blank?34 puts "\nUSAGE: rake ds:reindex[Model]"35 elsif args[:model] == 'all'36 @migrator.reindex_all37 else38 @migrator.reindex_one(args[:model].constantize)39 end40 end41 desc 'Create SOLR Core (Normally not needed) -- pass in a model name (all creates everything)'42 task :create_core, [:model] => :configure do |_t, args|43 if args[:model].blank?44 puts "\nUSAGE: rake ds:create_core[Model]"45 else46 @migrator.create_solr_core(args[:model].constantize)47 end48 end49 desc 'Load the seed data from ks/seeds.rb'50 task seed: :environment do51 seed_file = Rails.root.join('ks', 'seeds.rb')52 load(seed_file) if seed_file.exist?53 end54 if defined?(ParallelTests)55 namespace :parallel do56 desc 'create test keyspaces via ds:create --> ds:parallel:create[num_cpus]'57 task :create, :count do |_t, args|58 ParallelTests::Tasks.run_in_parallel("rake ds:create RAILS_ENV=#{ParallelTests::Tasks.rails_env}", args)59 end60 desc 'drop test keyspaces via ds:drop --> ds:parallel:drop[num_cpus]'61 task :drop, :count do |_t, args|62 ParallelTests::Tasks.run_in_parallel("rake ds:drop RAILS_ENV=#{ParallelTests::Tasks.rails_env}", args)63 end64 desc 'update test keyspaces via ds:migrate --> ds:parallel:migrate[num_cpus]'65 task :migrate, :count do |_t, args|66 args = args.to_hash.merge(non_parallel: true)67 ParallelTests::Tasks.run_in_parallel("rake ds:migrate RAILS_ENV=#{ParallelTests::Tasks.rails_env}", args)68 end69 end70 end71end...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!