Best Parallel_tests_ruby code snippet using ParallelTests.count
tasks.rb
Source:tasks.rb
...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...
parallel_tests_spec.rb
Source:parallel_tests_spec.rb
1require File.dirname(__FILE__) + '/spec_helper'2describe ParallelTests do3 test_tests_in_groups(ParallelTests, 'test', '_test.rb')4 describe :parse_rake_args do5 it "should return the count" do6 args = {:count => 2}7 ParallelTests.parse_rake_args(args).should == [2, '', ""]8 end9 it "should default to the prefix" do10 args = {:count => "models"}11 ParallelTests.parse_rake_args(args).should == [Parallel.processor_count, "models", ""]12 end13 it "should return the count and prefix" do14 args = {:count => 2, :path_prefix => "models"}15 ParallelTests.parse_rake_args(args).should == [2, "models", ""]16 end17 it "should return the count, prefix, and options" do18 args = {:count => 2, :path_prefix => "plain", :options => "-p default" }19 ParallelTests.parse_rake_args(args).should == [2, "plain", "-p default"]20 end21 end22 describe :run_tests do23 it "uses TEST_ENV_NUMBER=blank when called for process 0" do24 ParallelTests.should_receive(:open).with{|x,y|x=~/TEST_ENV_NUMBER= /}.and_return mocked_process25 ParallelTests.run_tests(['xxx'],0,'')26 end27 it "uses TEST_ENV_NUMBER=2 when called for process 1" do28 ParallelTests.should_receive(:open).with{|x,y| x=~/TEST_ENV_NUMBER=2/}.and_return mocked_process29 ParallelTests.run_tests(['xxx'],1,'')30 end31 it "uses options" do32 ParallelTests.should_receive(:open).with{|x,y| x=~ %r{ruby -Itest -v}}.and_return mocked_process...
count
Using AI Code Generation
1puts ParallelTests.count("1.rb")2puts ParallelTests.count("2.rb")3puts ParallelTests.count("3.rb")4puts ParallelTests.count("4.rb")5puts ParallelTests.count("5.rb")6puts ParallelTests.count("6.rb")7puts ParallelTests.count("7.rb")8puts ParallelTests.count("8.rb")9puts ParallelTests.count("9.rb")10puts ParallelTests.count("10.rb")
count
Using AI Code Generation
1puts ParallelTests.count_tests_in_file(ARGV[0])2puts ParallelTests.run_tests_in_parallel(ARGV[0])3puts ParallelTests.run_tests_in_sequence(ARGV[0])4puts ParallelTests.run_tests_in_parallel(ARGV[0])5puts ParallelTests.run_tests_in_sequence(ARGV[0])6puts ParallelTests.run_tests_in_parallel(ARGV[0])7puts ParallelTests.run_tests_in_sequence(ARGV[0])8puts ParallelTests.run_tests_in_parallel(ARGV[0])9puts ParallelTests.run_tests_in_sequence(ARGV[0])10puts ParallelTests.run_tests_in_sequence(
count
Using AI Code Generation
1count = ParallelTests::Test::Runner.count_tests_in_file('1.rb')2count = ParallelTests::Test::Runner.count_tests_in_file('2.rb')3count = ParallelTests::Test::Runner.count_tests_in_file('3.rb')4count = ParallelTests::Test::Runner.count_tests_in_file('4.rb')5count = ParallelTests::Test::Runner.count_tests_in_file('5.rb')6count = ParallelTests::Test::Runner.count_tests_in_file('6.rb')7count = ParallelTests::Test::Runner.count_tests_in_file('7.rb')8count = ParallelTests::Test::Runner.count_tests_in_file('8.rb')9puts ParallelTests.count("6.rb")10puts ParallelTests.count("7.rb")11puts ParallelTests.count("8.rb")12puts ParallelTests.count("9.rb")13puts ParallelTests.count("10.rb")
count
Using AI Code Generation
1puts ParallelTests.count_tests_in_file(ARGV[0])2puts ParallelTests.run_tests_in_parallel(ARGV[0])3puts ParallelTests.run_tests_in_sequence(ARGV[0])4puts ParallelTests.run_tests_in_parallel(ARGV[0])5puts ParallelTests.run_tests_in_sequence(ARGV[0])6puts ParallelTests.run_tests_in_parallel(ARGV[0])7puts ParallelTests.run_tests_in_sequence(ARGV[0])8puts ParallelTests.run_tests_in_parallel(ARGV[0])9puts ParallelTests.run_tests_in_sequence(ARGV[0])10puts ParallelTests.run_tests_in_sequence(
count
Using AI Code Generation
1puts ParallelTests.count("1.rb")2puts ParallelTests.count("2.rb")3puts ParallelTests.count("3.rb")4puts ParallelTests.count("4.rb")5puts ParallelTests.count("5.rb")6puts ParallelTests.count("6.rb")7puts ParallelTests.count("7.rb")8puts ParallelTests.count("8.rb")9puts ParallelTests.count("9.rb")10puts ParallelTests.count("10.rb")
count
Using AI Code Generation
1puts ParallelTests.count_tests_in_file(ARGV[0])2puts ParallelTests.run_tests_in_parallel(ARGV[0])3puts ParallelTests.run_tests_in_sequence(ARGV[0])4puts ParallelTests.run_tests_in_parallel(ARGV[0])5puts ParallelTests.run_tests_in_sequence(ARGV[0])6puts ParallelTests.run_tests_in_parallel(ARGV[0])7puts ParallelTests.run_tests_in_sequence(ARGV[0])8puts ParallelTests.run_tests_in_parallel(ARGV[0])9puts ParallelTests.run_tests_in_sequence(ARGV[0])10puts ParallelTests.run_tests_in_sequence(
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!!