Best Parallel_tests_ruby code snippet using ParallelTests.Test.executable
tasks.rb
Source:tasks.rb
...11 end12 end13 def run_in_parallel(cmd, options={})14 count = " -n #{options[:count]}" unless options[:count].to_s.empty?15 executable = File.expand_path("../../../bin/parallel_test", __FILE__)16 command = "#{Shellwords.escape executable} --exec '#{cmd}'#{count}#{' --non-parallel' if options[:non_parallel]}"17 command = ParallelTests.with_ruby_binary(command)18 abort unless system(command)19 end20 # this is a crazy-complex solution for a very simple problem:21 # removing certain lines from the output without changing the exit-status22 # normally I'd not do this, but it has been lots of fun and a great learning experience :)23 #24 # - sed does not support | without -r25 # - grep changes 0 exitstatus to 1 if nothing matches26 # - sed changes 1 exitstatus to 027 # - pipefail makes pipe fail with exitstatus of first failed command28 # - pipefail is not supported in (zsh)29 # - defining a new rake task like silence_schema would force users to load parallel_tests in test env30 # - do not use ' since run_in_parallel uses them to quote stuff31 # - simple system "set -o pipefail" returns nil even though set -o pipefail exists with 032 def suppress_output(command, ignore_regex)33 activate_pipefail = "set -o pipefail"34 remove_ignored_lines = %Q{(grep -v "#{ignore_regex}" || test 1)}35 if File.executable?('/bin/bash') && system('/bin/bash', '-c', "#{activate_pipefail} 2>/dev/null && test 1")36 # We need to shell escape single quotes (' becomes '"'"') because37 # run_in_parallel wraps command in single quotes38 %Q{/bin/bash -c '"'"'#{activate_pipefail} && (#{command}) | #{remove_ignored_lines}'"'"'}39 else40 command41 end42 end43 def suppress_schema_load_output(command)44 ParallelTests::Tasks.suppress_output(command, "^ ->\\|^-- ")45 end46 def check_for_pending_migrations47 ["db:abort_if_pending_migrations", "app:db:abort_if_pending_migrations"].each do |abort_migrations|48 if Rake::Task.task_defined?(abort_migrations)49 Rake::Task[abort_migrations].invoke50 break51 end52 end53 end54 # parallel:spec[:count, :pattern, :options]55 def parse_args(args)56 # order as given by user57 args = [args[:count], args[:pattern], args[:options]]58 # count given or empty ?59 # parallel:spec[2,models,options]60 # parallel:spec[,models,options]61 count = args.shift if args.first.to_s =~ /^\d*$/62 num_processes = count.to_i unless count.to_s.empty?63 pattern = args.shift64 options = args.shift65 [num_processes, pattern.to_s, options.to_s]66 end67 end68 end69end70namespace :parallel do71 desc "Setup test databases via db:setup --> parallel:setup[num_cpus]"72 task :setup, :count do |_,args|73 command = "rake db:setup RAILS_ENV=#{ParallelTests::Tasks.rails_env}"74 ParallelTests::Tasks.run_in_parallel(ParallelTests::Tasks.suppress_schema_load_output(command), args)75 end76 desc "Create test databases via db:create --> parallel:create[num_cpus]"77 task :create, :count do |_,args|78 ParallelTests::Tasks.run_in_parallel("rake db:create RAILS_ENV=#{ParallelTests::Tasks.rails_env}", args)79 end80 desc "Drop test databases via db:drop --> parallel:drop[num_cpus]"81 task :drop, :count do |_,args|82 ParallelTests::Tasks.run_in_parallel("rake db:drop RAILS_ENV=#{ParallelTests::Tasks.rails_env} DISABLE_DATABASE_ENVIRONMENT_CHECK=1", args)83 end84 desc "Update test databases by dumping and loading --> parallel:prepare[num_cpus]"85 task(:prepare, [:count]) do |_,args|86 ParallelTests::Tasks.check_for_pending_migrations87 if defined?(ActiveRecord) && ActiveRecord::Base.schema_format == :ruby88 # dump then load in parallel89 Rake::Task['db:schema:dump'].invoke90 Rake::Task['parallel:load_schema'].invoke(args[:count])91 else92 # there is no separate dump / load for schema_format :sql -> do it safe and slow93 args = args.to_hash.merge(:non_parallel => true) # normal merge returns nil94 taskname = Rake::Task.task_defined?('db:test:prepare') ? 'db:test:prepare' : 'app:db:test:prepare'95 ParallelTests::Tasks.run_in_parallel("rake #{taskname}", args)96 end97 end98 # when dumping/resetting takes too long99 desc "Update test databases via db:migrate --> parallel:migrate[num_cpus]"100 task :migrate, :count do |_,args|101 ParallelTests::Tasks.run_in_parallel("rake db:migrate RAILS_ENV=#{ParallelTests::Tasks.rails_env}", args)102 end103 # just load the schema (good for integration server <-> no development db)104 desc "Load dumped schema for test databases via db:schema:load --> parallel:load_schema[num_cpus]"105 task :load_schema, :count do |_,args|106 command = "rake #{ParallelTests::Tasks.purge_before_load} db:schema:load RAILS_ENV=#{ParallelTests::Tasks.rails_env} DISABLE_DATABASE_ENVIRONMENT_CHECK=1"107 ParallelTests::Tasks.run_in_parallel(ParallelTests::Tasks.suppress_schema_load_output(command), args)108 end109 # load the structure from the structure.sql file110 desc "Load structure for test databases via db:structure:load --> parallel:load_structure[num_cpus]"111 task :load_structure, :count do |_,args|112 ParallelTests::Tasks.run_in_parallel("rake #{ParallelTests::Tasks.purge_before_load} db:structure:load RAILS_ENV=#{ParallelTests::Tasks.rails_env} DISABLE_DATABASE_ENVIRONMENT_CHECK=1", args)113 end114 desc "Load the seed data from db/seeds.rb via db:seed --> parallel:seed[num_cpus]"115 task :seed, :count do |_,args|116 ParallelTests::Tasks.run_in_parallel("rake db:seed RAILS_ENV=#{ParallelTests::Tasks.rails_env}", args)117 end118 desc "Launch given rake command in parallel"119 task :rake, :command, :count do |_, args|120 ParallelTests::Tasks.run_in_parallel("RAILS_ENV=#{ParallelTests::Tasks.rails_env} rake #{args.command}", args)121 end122 ['test', 'spec', 'features', 'features-spinach'].each do |type|123 desc "Run #{type} in parallel with parallel:#{type}[num_cpus]"124 task type, [:count, :pattern, :options] do |t, args|125 ParallelTests::Tasks.check_for_pending_migrations126 $LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), '..'))127 require "parallel_tests"128 count, pattern, options = ParallelTests::Tasks.parse_args(args)129 test_framework = {130 'spec' => 'rspec',131 'test' => 'test',132 'features' => 'cucumber',133 'features-spinach' => 'spinach',134 }[type]135 if test_framework == 'spinach'136 type = 'features'137 end138 executable = File.join(File.dirname(__FILE__), '..', '..', 'bin', 'parallel_test')139 command = "#{Shellwords.escape executable} #{type} --type #{test_framework} " \140 "-n #{count} " \141 "--pattern '#{pattern}' " \142 "--test-options '#{options}'"143 command = ParallelTests.with_ruby_binary(command)144 abort unless system(command) # allow to chain tasks e.g. rake parallel:spec parallel:features145 end146 end147end...
executable
Using AI Code Generation
1ParallelTests::CLI.new.run(['test'])2ParallelTests::CLI.new.run(['test', '-n', '2'])3ParallelTests::CLI.new.run(['test', '-n', '2', '-o', 'test_results.xml'])4ParallelTests::CLI.new.run(['test', '-n', '2', '-o', 'test_results.xml', '-r'])5ParallelTests::CLI.new.run(['test', '-n', '2', '-o', 'test_results.xml', '-r', '--serialize-stdout'])6ParallelTests::CLI.new.run(['test', '-n', '4', '-o', 'test_results.xml', '-r', '--serialize-stdout', '--group-y', 'runtime'])7ParallelTests::CLI.new.run(['test', '-n', '2', '-o', 'test_results.xml', '-r', '--serialize-stdout', '--group-by', 'runtime'])8ParallelTests::CLI.new.run(['test', '-n', '2', '-o', 'test_results.xml', '-r',
executable
Using AI Code Generation
1ParallelTests::Test::executable('rspec')2ParallelTests::Test::executable('rspec', 2)3ParallelTests::Test::executable('rspec', 2, 'spec')4ParallelTests::Test::executable('rspec', 2, 'spec', 'spec2')5ParallelTests::Test::executable('rspec', 2, 'spec', 'spec2', 'spec3')6ParallelTests::Test::executable('rspec', 2, 'spec', 'spec2', 'spec3', 'spec4')7ParallelTests::Test::executable('rspec', 2, 'spec', 'spec2', 'spec3', 'spec4', 'spec5')8ParallelTests::Test::executable('rspec', 2, 'spec', 'spec2', 'spec3', 'spec4', 'spec5', 'spec6')9ParallelTests::Test::executable('rspec', 2, 'spec', 'spec2', 'spec3', 'spec4', 'spec5', 'spec6', 'spec7')
executable
Using AI Code Generation
1ParallelTests::Test::executable('rspec')2ParallelTests::Test::executable('rspec', 2)3ParallelTests::Test::executable('rspec', 2, 'spec')4ParallelTests::Test::executable('rspec', 2, 'spec', 'spec2')5ParallelTests::Test::executable('rspec', 2, 'spec', 'spec2', 'spec3')6ParallelTests::Test::executable('rspec', 2, 'spec', 'spec2', 'spec3', 'spec4')7ParallelTests::Test::executable('rspec', 2, 'spec', 'spec2', 'spec3', 'spec4', 'spec5')8ParallelTests::Test::executable('rspec', 2, 'spec', 'spec2', 'spec3', 'spec4', 'spec5', 'spec6')9ParallelTests::Test::executable('rspec', 2, 'spec', 'spec2', 'spec3', 'spec4', 'spec5', 'spec6', 'spec7')
executable
Using AI Code Generation
1ParallelTests.determine_number_of_processes(2)2ParallelTests::Test::Runner.tests_in_groups('test', 2)3ParallelTests::Test::Runner.run_tests_in_parallel(ParallelTests::Test::Runner.tests_in_groups('test', 2))4ParallelTests.determine_number_of_processes(2)5ParallelTests::Test::Runner.tests_in_groups('test', 2)6ParallelTests::Test::Runner.run_tests_in_parallel(ParallelTests::Test::Runner.tests_in_groups('test', 2))
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!!