Class: Actir::ParallelTests::Test::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/actir/parallel_tests/test/runner.rb

Direct Known Subclasses

Rerunner

Constant Summary collapse

NAME =
'Test'

Class Method Summary collapse

Class Method Details

.execute_command(cmd, process_number, num_processes, options) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/actir/parallel_tests/test/runner.rb', line 83

def execute_command(cmd, process_number, num_processes, options)
  env = (options[:env] || {}).merge(
    #"TEST_ENV_NUMBER" => test_env_number(process_number),
    "env" => $env,
    "TEST_ENV_NUMBER" => process_number,
    "PARALLEL_TEST_GROUPS" => num_processes
  )
  cmd = "nice #{cmd}" if options[:nice]
  cmd = "#{cmd} 2>&1" if options[:combine_stderr]
  puts cmd if options[:verbose]

  execute_command_and_capture_output(env, cmd, options[:serialize_stdout])
end

.execute_command_and_capture_output(env, cmd, silence) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/actir/parallel_tests/test/runner.rb', line 97

def execute_command_and_capture_output(env, cmd, silence)
  # make processes descriptive / visible in ps -ef
  separator = ';'
  exports = env.map do |k,v|
    "export #{k}=#{v}"
  end.join(separator)
  cmd = "#{exports}#{separator}#{cmd}"
  output = open("|#{cmd}", "r") { |output| capture_output(output, silence) }
  #modify by Hub
  #exitstatus = $?.exitstatus
  #"$?.exitstatus" 返回的值有时有问题,不能明确标示用例执行结果是否成功
  #改成判断结果数据中是否有failure和error
  exitstatus = get_test_failed_num(find_results(output).join)
  {:stdout => output, :exit_status => exitstatus}
end

.find_results(test_output) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/actir/parallel_tests/test/runner.rb', line 113

def find_results(test_output)
  test_output.split("\n").map {|line|
    line.gsub!(/\e\[\d+m/,'')
    next unless line_is_result?(line)
    line
  }.compact
end

.line_is_result?(line) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
# File 'lib/actir/parallel_tests/test/runner.rb', line 58

def line_is_result?(line)
  line.gsub!(/[.F*]/,'')
  line =~ /\d+ failure/
end

.nameObject

— usually overwritten by other runners



10
11
12
# File 'lib/actir/parallel_tests/test/runner.rb', line 10

def name
  NAME
end

.run_tests(test_files, process_number, num_processes, options, address) ⇒ Object

modify by Hub add param address to ruby script as ARGV modify cmd to exec ruby test script



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/actir/parallel_tests/test/runner.rb', line 28

def run_tests(test_files, process_number, num_processes, options, address)
  #require_list = test_files.map { |file| file.sub(" ", "\\ ") }.join(" ")
  #cmd = "#{executable} -Itest -e '%w[#{require_list}].each { |f| require %{./\#{f}}}' #{address}"
  #execute_command(cmd, process_number, num_processes, options)

  cmd = ""
  #如果有-n选项,则只运行指定的测试用例
  if options[:testcase]
    raise "more than one file was choosen" if test_files.size != 1 
    cmd = "#{executable} #{test_files[0]} -n #{options[:testcase]} #{address}"
  else
    test_files.each do |file|
      cmd += "#{executable} #{file} #{address};"
    end 
    cmd += "\n"
  end

  result = execute_command(cmd, process_number, num_processes, options)
  #记录log
  if options[:log]
    log_str = "[run_tests]: \n" + result[:stdout]
    Actir::ParallelTests::Test::Logger.log(log_str, process_number)
  end

  #从result中获取执行结果用于生成测试报告
  Actir::ParallelTests::Test::Result.get_testsuite_detail(result)

  result
end

.summarize_results(results) ⇒ Object



125
126
127
128
129
# File 'lib/actir/parallel_tests/test/runner.rb', line 125

def summarize_results(results)
  sums = sum_up_results(results)
  sums.to_a.map{|word, number|  "#{number} #{word}#{'s' if number != 1}" }.join(', ')
  #sums.sort.map{|word, number|  "#{number} #{word}#{'s' if number != 1}" }.join(', ')
end

.test_env_number(process_number) ⇒ Object



121
122
123
# File 'lib/actir/parallel_tests/test/runner.rb', line 121

def test_env_number(process_number)
  process_number == 0 ? '' : process_number + 1
end

.test_file_nameObject



21
22
23
# File 'lib/actir/parallel_tests/test/runner.rb', line 21

def test_file_name
  "test"
end

.test_suffixObject

modify by Hub 修改正则表达式使得使用我们目前的测试脚本文件命名 : test_xxx.rb



16
17
18
19
# File 'lib/actir/parallel_tests/test/runner.rb', line 16

def test_suffix
 # /_(test|spec).rb$/
 /test.*\.rb$/
end

.tests_in_groups(tests, num_groups, options = {}) ⇒ Object

finds all tests and partitions them into groups



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/actir/parallel_tests/test/runner.rb', line 66

def tests_in_groups(tests, num_groups, options={})
  tests = find_tests(tests, options)

  case options[:group_by]
  when :found
    tests.map! { |t| [t, 1] }
  when :filesize
    sort_by_filesize(tests)
  when nil
    sort_by_filesize(tests)
  else
    raise ArgumentError, "Unsupported option #{options[:group_by]}"
  end

  Grouper.in_even_groups_by_size(tests, num_groups, options)
end