How to use run method of ParallelTests Package

Best Parallel_tests_ruby code snippet using ParallelTests.run

tasks.rb

Source:tasks.rb Github

copy

Full Screen

...21 if Gem::Version.new(Rails.version) > Gem::Version.new('4.2.0')22 Rake::Task.task_defined?('db:purge') ? 'db:purge' : 'app:db:purge'23 end24 end25 def run_in_parallel(cmd, options={})26 load_lib27 count = " -n #{options[:count]}" unless options[:count].to_s.empty?28 # Using the relative path to find the binary allow to run a specific version of it29 executable = File.expand_path("../../../bin/parallel_test", __FILE__)30 command = "#{ParallelTests.with_ruby_binary(Shellwords.escape(executable))} --exec '#{cmd}'#{count}#{' --non-parallel' if options[:non_parallel]}"31 abort unless system(command)32 end33 # this is a crazy-complex solution for a very simple problem:34 # removing certain lines from the output without changing the exit-status35 # normally I'd not do this, but it has been lots of fun and a great learning experience :)36 #37 # - sed does not support | without -r38 # - grep changes 0 exitstatus to 1 if nothing matches39 # - sed changes 1 exitstatus to 040 # - pipefail makes pipe fail with exitstatus of first failed command41 # - pipefail is not supported in (zsh)42 # - defining a new rake task like silence_schema would force users to load parallel_tests in test env43 # - do not use ' since run_in_parallel uses them to quote stuff44 # - simple system "set -o pipefail" returns nil even though set -o pipefail exists with 045 def suppress_output(command, ignore_regex)46 activate_pipefail = "set -o pipefail"47 remove_ignored_lines = %Q{(grep -v "#{ignore_regex}" || test 1)}48 if File.executable?('/bin/bash') && system('/bin/bash', '-c', "#{activate_pipefail} 2>/dev/null && test 1")49 # We need to shell escape single quotes (' becomes '"'"') because50 # run_in_parallel wraps command in single quotes51 %Q{/bin/bash -c '"'"'#{activate_pipefail} && (#{command}) | #{remove_ignored_lines}'"'"'}52 else53 command54 end55 end56 def suppress_schema_load_output(command)57 ParallelTests::Tasks.suppress_output(command, "^ ->\\|^-- ")58 end59 def check_for_pending_migrations60 ["db:abort_if_pending_migrations", "app:db:abort_if_pending_migrations"].each do |abort_migrations|61 if Rake::Task.task_defined?(abort_migrations)62 Rake::Task[abort_migrations].invoke63 break64 end65 end66 end67 # parallel:spec[:count, :pattern, :options, :pass_through]68 def parse_args(args)69 # order as given by user70 args = [args[:count], args[:pattern], args[:options], args[:pass_through]]71 # count given or empty ?72 # parallel:spec[2,models,options]73 # parallel:spec[,models,options]74 count = args.shift if args.first.to_s =~ /^\d*$/75 num_processes = count.to_i unless count.to_s.empty?76 pattern = args.shift77 options = args.shift78 pass_through = args.shift79 [num_processes, pattern.to_s, options.to_s, pass_through.to_s]80 end81 end82 end83end84namespace :parallel do85 desc "Setup test databases via db:setup --> parallel:setup[num_cpus]"86 task :setup, :count do |_,args|87 command = "#{ParallelTests::Tasks.rake_bin} db:setup RAILS_ENV=#{ParallelTests::Tasks.rails_env}"88 ParallelTests::Tasks.run_in_parallel(ParallelTests::Tasks.suppress_schema_load_output(command), args)89 end90 desc "Create test databases via db:create --> parallel:create[num_cpus]"91 task :create, :count do |_,args|92 ParallelTests::Tasks.run_in_parallel(93 "#{ParallelTests::Tasks.rake_bin} db:create RAILS_ENV=#{ParallelTests::Tasks.rails_env}", args)94 end95 desc "Drop test databases via db:drop --> parallel:drop[num_cpus]"96 task :drop, :count do |_,args|97 ParallelTests::Tasks.run_in_parallel(98 "#{ParallelTests::Tasks.rake_bin} db:drop RAILS_ENV=#{ParallelTests::Tasks.rails_env} " \99 "DISABLE_DATABASE_ENVIRONMENT_CHECK=1", args)100 end101 desc "Update test databases by dumping and loading --> parallel:prepare[num_cpus]"102 task(:prepare, [:count]) do |_,args|103 ParallelTests::Tasks.check_for_pending_migrations104 if defined?(ActiveRecord::Base) && [:ruby, :sql].include?(ActiveRecord::Base.schema_format)105 # fast: dump once, load in parallel106 type = (ActiveRecord::Base.schema_format == :ruby ? "schema" : "structure")107 Rake::Task["db:#{type}:dump"].invoke108 # remove database connection to prevent "database is being accessed by other users"109 ActiveRecord::Base.remove_connection if ActiveRecord::Base.configurations.any?110 Rake::Task["parallel:load_#{type}"].invoke(args[:count])111 else112 # slow: dump and load in in serial113 args = args.to_hash.merge(:non_parallel => true) # normal merge returns nil114 task_name = Rake::Task.task_defined?('db:test:prepare') ? 'db:test:prepare' : 'app:db:test:prepare'115 ParallelTests::Tasks.run_in_parallel("#{ParallelTests::Tasks.rake_bin} #{task_name}", args)116 next117 end118 end119 # when dumping/resetting takes too long120 desc "Update test databases via db:migrate --> parallel:migrate[num_cpus]"121 task :migrate, :count do |_,args|122 ParallelTests::Tasks.run_in_parallel(123 "#{ParallelTests::Tasks.rake_bin} db:migrate RAILS_ENV=#{ParallelTests::Tasks.rails_env}", args)124 end125 desc "Rollback test databases via db:rollback --> parallel:rollback[num_cpus]"126 task :rollback, :count do |_,args|127 ParallelTests::Tasks.run_in_parallel(128 "#{ParallelTests::Tasks.rake_bin} db:rollback RAILS_ENV=#{ParallelTests::Tasks.rails_env}", args)129 end130 # just load the schema (good for integration server <-> no development db)131 desc "Load dumped schema for test databases via db:schema:load --> parallel:load_schema[num_cpus]"132 task :load_schema, :count do |_,args|133 command = "#{ParallelTests::Tasks.rake_bin} #{ParallelTests::Tasks.purge_before_load} " \134 "db:schema:load RAILS_ENV=#{ParallelTests::Tasks.rails_env} DISABLE_DATABASE_ENVIRONMENT_CHECK=1"135 ParallelTests::Tasks.run_in_parallel(ParallelTests::Tasks.suppress_schema_load_output(command), args)136 end137 # load the structure from the structure.sql file138 desc "Load structure for test databases via db:structure:load --> parallel:load_structure[num_cpus]"139 task :load_structure, :count do |_,args|140 ParallelTests::Tasks.run_in_parallel(141 "#{ParallelTests::Tasks.rake_bin} #{ParallelTests::Tasks.purge_before_load} " \142 "db:structure:load RAILS_ENV=#{ParallelTests::Tasks.rails_env} DISABLE_DATABASE_ENVIRONMENT_CHECK=1", args)143 end144 desc "Load the seed data from db/seeds.rb via db:seed --> parallel:seed[num_cpus]"145 task :seed, :count do |_,args|146 ParallelTests::Tasks.run_in_parallel(147 "#{ParallelTests::Tasks.rake_bin} db:seed RAILS_ENV=#{ParallelTests::Tasks.rails_env}", args)148 end149 desc "Launch given rake command in parallel"150 task :rake, :command, :count do |_, args|151 ParallelTests::Tasks.run_in_parallel(152 "RAILS_ENV=#{ParallelTests::Tasks.rails_env} #{ParallelTests::Tasks.rake_bin} " \153 "#{args.command}", args)154 end155 ['test', 'spec', 'features', 'features-spinach'].each do |type|156 desc "Run #{type} in parallel with parallel:#{type}[num_cpus]"157 task type, [:count, :pattern, :options, :pass_through] do |t, args|158 ParallelTests::Tasks.check_for_pending_migrations159 ParallelTests::Tasks.load_lib160 count, pattern, options, pass_through = ParallelTests::Tasks.parse_args(args)161 test_framework = {162 'spec' => 'rspec',163 'test' => 'test',164 'features' => 'cucumber',165 'features-spinach' => 'spinach',166 }[type]167 if test_framework == 'spinach'168 type = 'features'169 end170 # Using the relative path to find the binary allow to run a specific version of it171 executable = File.join(File.dirname(__FILE__), '..', '..', 'bin', 'parallel_test')172 command = "#{ParallelTests.with_ruby_binary(Shellwords.escape(executable))} #{type} " \173 "--type #{test_framework} " \174 "-n #{count} " \175 "--pattern '#{pattern}' " \176 "--test-options '#{options}' " \177 "#{pass_through}"178 abort unless system(command) # allow to chain tasks e.g. rake parallel:spec parallel:features179 end180 end181end...

Full Screen

Full Screen

parallel_tests_spec.rb

Source:parallel_tests_spec.rb Github

copy

Full Screen

...43 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 pending74EOF...

Full Screen

Full Screen

run

Using AI Code Generation

copy

Full Screen

1ParallelTests.run(ARGV)2 def self.run(argv)3 Parallel.map(argv) do |arg|4tests = %w(test1 test2 test3)5Parallel.map(tests) do |test|6tests = %w(test1 test2 test3)7Parallel.map(tests) do |test|

Full Screen

Full Screen

run

Using AI Code Generation

copy

Full Screen

1output = pt.run(2, 'test_*.rb')2 def run(processes, file_pattern)3 output << Open3.capture3(command)[0]4 threads.each(&:join)5 assert_equal(1, 1)6 assert_equal(1, 1)7 assert_equal(1, 1)8 assert_equal(1, 1)

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful