Best Parallel_tests_ruby code snippet using ParallelTests.Test.test_env_number
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...
worker_spec.rb
Source:worker_spec.rb
1require './lib/zeus/parallel_tests/worker'2describe Zeus::ParallelTests::Worker do3 describe '.run' do4 let(:cli_argv) { ['rspec', 'spec/models/model_spec.rb'] }5 let(:cli_env) { { 'TEST_ENV_NUMBER' => '3' } }6 let(:worker) { double('worker', spawn: 0) }7 before { allow(Zeus::ParallelTests::Worker).to receive_messages(new: worker) }8 subject { Zeus::ParallelTests::Worker.run(cli_argv, cli_env) }9 it 'creates instance of worker' do10 expect(Zeus::ParallelTests::Worker).to receive(:new)11 .with('rspec', cli_env, ['spec/models/model_spec.rb'])12 .and_return(worker)13 subject14 end15 it 'does not modify original env and argv' do16 subject17 expect(cli_argv).to eq(['rspec', 'spec/models/model_spec.rb'])18 expect(cli_env).to eq('TEST_ENV_NUMBER' => '3')19 end20 it 'returns exit code' do21 expect(subject).to eq(0)22 end23 end24 describe '#spawn' do25 subject { worker.spawn }26 let(:worker) { Zeus::ParallelTests::Worker.new('rspec', cli_env, ['spec/file_spec.rb']) }27 let(:cli_env) { { 'TEST_ENV_NUMBER' => 2, 'PARALLEL_TEST_GROUPS' => 4 } }28 let(:argv_file) { double('argv_file', path: 'argv_file_path', unlink: true, puts: nil, close: nil) }29 before do30 allow(Tempfile).to receive_messages(new: argv_file)31 allow(worker).to receive_messages(system: true)32 end33 it 'writes args to file' do34 expect(argv_file).to receive(:puts).with('spec/file_spec.rb')35 subject36 end37 it 'spawns worker and passes TEST_ENV_NUMBER, PARALLEL_TEST_GROUPS and argv file path' do38 expect(worker).to receive(:system).with('zeus parallel_rspec_worker 2 4 argv_file_path')39 subject40 end41 it 'removes argv_file after run' do42 expect(argv_file).to receive(:unlink)43 subject44 end45 it 'returns exit code' do46 system 'true'47 expect(worker).to receive(:system) { allow($CHILD_STATUS).to receive_messages(to_i: 1) }48 expect(subject).to eq(1)49 end50 end51end...
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!!