Class: Berl::BehatRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/berl/behat_runner.rb

Overview

Service to run Behat tests

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(num_of_workers, suites, databases) ⇒ BehatRunner

Returns a new instance of BehatRunner.



8
9
10
11
12
13
14
15
# File 'lib/berl/behat_runner.rb', line 8

def initialize(num_of_workers, suites, databases)
  @num_of_succeed = 0
  @num_of_failed = 0
  @num_of_workers = num_of_workers
  @suites = suites
  @databases = databases
  @output = {}
end

Instance Attribute Details

#num_of_failedObject (readonly)

Returns the value of attribute num_of_failed.



6
7
8
# File 'lib/berl/behat_runner.rb', line 6

def num_of_failed
  @num_of_failed
end

#num_of_succeedObject (readonly)

Returns the value of attribute num_of_succeed.



6
7
8
# File 'lib/berl/behat_runner.rb', line 6

def num_of_succeed
  @num_of_succeed
end

Instance Method Details

#handle_finish(_, _, result) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/berl/behat_runner.rb', line 54

def handle_finish(_, _, result)
  if result['status'] == 'success'
    @num_of_succeed += 1
    puts "#{result['suite_name']} (#{num_of_executed}/#{@suites.count})"

    return
  end

  @num_of_failed += 1
  @output[result['suite_name']] = result['output']
  puts "#{result['suite_name']} (#{num_of_executed}/#{@suites.count})"
end


73
74
75
76
77
78
79
# File 'lib/berl/behat_runner.rb', line 73

def print_errors
  puts "\n\n 🚨 Errors for failed suites:"
  @output.each do |suite_name, output|
    puts "\n❌ #{suite_name}"
    puts output
  end
end

#startObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/berl/behat_runner.rb', line 17

def start
  Parallel.each(
    @suites,
    in_processes: @num_of_workers,
    finish: method(:handle_finish)
  ) do |suite|
    database = @databases[Parallel.worker_number]
    cache = "cache_#{Parallel.worker_number}"

    behat_cmd = build_behat_cmd(database, cache, suite)
    Open3.popen3(behat_cmd) do |_, stdout, _, wait_thr|
      result = {}
      result['output'] = []

      result['output'].push(*stdout.readlines) if wait_thr.value != 0

      result['suite_name'] = suite
      result['status'] = wait_thr.value == 0 ? 'success' : 'fail'

      result
    end
  end
end