Module: TDRunner::TDRunnerMonitor

Included in:
Main
Defined in:
lib/tdrunner_monitor.rb

Overview

TDRunner monitor module for controlling the SIERRA execution

Instance Method Summary collapse

Instance Method Details

#add_failed_testunit_tests(tdrunner_parameters, case_contents_array) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/tdrunner_monitor.rb', line 25

def add_failed_testunit_tests(tdrunner_parameters,case_contents_array)
  if(tdrunner_parameters.failed_execution_profile != nil)

    add_tcs_to_failed_array(case_contents_array)

  end
end

#add_missing_methods(original_test_class, missing_methods, loaded_test_files) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/tdrunner_monitor.rb', line 204

def add_missing_methods(original_test_class,missing_methods,loaded_test_files)
  ObjectSpace.each_object( Class ){ | test_class |
    if test_class.ancestors.include?( Test::Unit::TestCase ) && test_class != Test::Unit::TestCase
      test_class.public_instance_methods( true ).sort.each{ | method |
        method = method.to_s # for ruby 1.9 compatibility
        if method =~ /^test_/
          missing_methods.each do |missing_method|
            if missing_method.to_s == method
              method_implementation=find_missing_method_from_ruby_file(method,loaded_test_files)
              original_test_class.class_eval("module TestModule_#{missing_method.to_s}
              #{method_implementation.to_s}
              end")
              original_test_class.class_eval("include TestModule_#{missing_method.to_s}")
            end
          end
        end
      }
    end
  }
  original_test_class
end

#add_missing_methods_in_to_test_class(test_class, tdrunner_parameters, loaded_test_files) ⇒ Object

adds missing test methods in to the test class based on tdrunner execution profile



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/tdrunner_monitor.rb', line 251

def add_missing_methods_in_to_test_class(test_class,tdrunner_parameters,loaded_test_files)

  f = File.open(tdrunner_parameters.execution_profile) or die "Unable to open file..."
  case_name_array=[]
  b_profile_file_exist=false
  f.each_line do |line|
    case_arr=line.split('=')
    if case_arr.length > 1
      case_name_array << case_arr[0].gsub('-','')
      b_profile_file_exist=true
    end
  end
  if b_profile_file_exist==true
    missing_methods=get_missing_methods(test_class, case_name_array)
    test_class=add_missing_methods(test_class,missing_methods,loaded_test_files)
  end
  test_class
end

#add_tcs_to_failed_array(case_contents_array) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tdrunner_monitor.rb', line 33

def add_tcs_to_failed_array(case_contents_array)

  failed_cases=[]
  $failed_tests ||= Array.new
  b_fail=false
  failed_level=0
  if case_contents_array.count > 0
    class_name = get_test_class(case_contents_array[0].at(0).to_s) + "=1"
    $failed_tests << class_name if case_contents_array[0].at(0).to_s.include? '('

    case_contents_array.reverse_each do |tcase|
      if tcase.at(3)==false || tcase.at(3)=="not run"
        class_name = get_test_class( tcase.at(2)) + "=1"
        failed_level=tcase.at(2).count( "-").to_i
        failed_cases << tcase.at(2)+ "=1" if tcase.at(2).include? '('
        b_fail=true if failed_level!=0
      end
      if b_fail==true and tcase.at(2).count( "-").to_i < failed_level and tcase.at(2).count( "-").to_i != 0 then
        failed_cases << tcase.at(2)
      elsif tcase.at(2).count( "-").to_i == 0 && b_fail==true
        class_name = get_test_class(tcase.at(2)) + "=1"
        failed_cases << tcase.at(2)+ "=1" if tcase.at(2).include? '('
        b_fail=false
      end
    end
  end
  $failed_tests << failed_cases.reverse

end

#exit_tdrunner_execution(message) ⇒ Object



137
138
139
# File 'lib/tdrunner_monitor.rb', line 137

def exit_tdrunner_execution(message)
  puts message
end

#find_missing_method_from_ruby_file(method, loaded_test_files, method_name = nil) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/tdrunner_monitor.rb', line 156

def find_missing_method_from_ruby_file(method, loaded_test_files, method_name=nil)
  found_implementation=Array.new
  found_setup_implementation=Array.new
  found_teardown_implementation=Array.new

  loaded_test_files.each do |test_file|
    if found_implementation == []
      f = File.open(test_file) or die "Unable to open file..."
      b_method_found=false

      f.each_line do |line|
        if b_method_found==true
          if line.include?('def ')
            b_method_found=false
          end
        end

        if b_method_found==true
          found_implementation << line
        end

        if line=~/def #{method}(.*)$/
          if method_name==nil
            found_implementation << line
            found_setup_implementation << find_missing_method_from_ruby_file('setup',test_file,"setup_#{method}")
            found_teardown_implementation << find_missing_method_from_ruby_file('teardown',test_file,"teardown_#{method}")
          else
            found_implementation << line.gsub(method, method_name)
          end
          b_method_found=true
        end
      end

      if b_method_found==true
        found_implementation=remove_extra_ends_from_implementation(found_implementation,1)
        b_method_found=false
      elsif found_implementation.length > 1 && b_method_found==false
        #found_implementation=remove_extra_ends_from_implementation(found_implementation,1)
        b_method_found=false
      end

    end
  end
  found_implementation << found_setup_implementation
  found_implementation << found_teardown_implementation
  found_implementation
end

#flatten_tests(tests) ⇒ Object

Removes duplicates and existing randomizations if found



272
273
274
275
276
277
278
279
280
# File 'lib/tdrunner_monitor.rb', line 272

def flatten_tests(tests)
  clean_test_arr=[]
  tests.each do |test_case|
    unless clean_test_arr.include? test_case
      clean_test_arr.push test_case
    end
  end
  clean_test_arr
end

#format_duration(seconds) ⇒ Object

Formater for displaying the execution time



565
566
567
568
# File 'lib/tdrunner_monitor.rb', line 565

def format_duration(seconds)
  m, s = seconds.divmod(60)
  "#{m}m#{'%.3f' % s}s"
end

#format_execution_duration(duration_value) ⇒ Object

Method for checking how many seconds TDRunner has run



127
128
129
130
131
132
133
134
135
# File 'lib/tdrunner_monitor.rb', line 127

def format_execution_duration(duration_value)
  secs=0
  if (/^\d+:\d+$/.match(duration_value.to_s))
    secs = (duration_value.split(':') [0].to_i*3600) + (duration_value.split(':') [1].to_i*60)
  elsif (/^\d+\.\d+$/.match(duration_value.to_s) || /^\d+$/.match(duration_value.to_s) )
    secs = 3600*duration_value.to_f
  end
  secs
end

#get_missing_methods(test_class, case_name_array) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/tdrunner_monitor.rb', line 226

def get_missing_methods(test_class, case_name_array)
  missing_methods_arr=Array.new
  case_name_array.each do |test_case|
    tc_line_arr=test_case.gsub(')','(').split('(')
    if tc_line_arr.length > 1
      if tc_line_arr[1].to_s==test_class.name.to_s
        b_method_exists=false
        test_class.public_instance_methods( true ).sort.each{ | method |
          method = method.to_s # for ruby 1.9 compatibility
          if method =~ /^test_/
            if tc_line_arr[0].to_s==method.to_s
              b_method_exists=true
            end
          end
        }
        if b_method_exists==false
          missing_methods_arr << tc_line_arr[0].to_s
        end
      end
    end
  end
  missing_methods_arr
end

#get_pos_string(pos) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/tdrunner_monitor.rb', line 63

def get_pos_string(pos)
  pos_str=""
  if(pos>0)
    pos_str=Array.new(pos, "-").join.to_s
  end
  return pos_str
end

#get_test_class(testcase_name) ⇒ Object



77
78
79
80
81
# File 'lib/tdrunner_monitor.rb', line 77

def get_test_class(testcase_name)
  name=testcase_name.gsub(/[)]/,'')
  name_arr=name.split('(')
  return name_arr[1].to_s
end

#get_test_classes(tests) ⇒ Object



282
283
284
285
286
287
288
289
290
# File 'lib/tdrunner_monitor.rb', line 282

def get_test_classes(tests)
  test_classes=[]
  tests.each do |test_case|
    if test_case.to_s.include?('(')==false
      test_classes.push test_case
    end
  end
  test_classes
end

#get_test_name(testcase_name) ⇒ Object



71
72
73
74
75
# File 'lib/tdrunner_monitor.rb', line 71

def get_test_name(testcase_name)
  name=testcase_name.gsub(/[)]/,'')
  name_arr=name.split('(')
  return name_arr[0].to_s
end

#is_class_included(case_contents_array, class_name) ⇒ Object



292
293
294
295
296
297
298
299
300
301
# File 'lib/tdrunner_monitor.rb', line 292

def is_class_included(case_contents_array,class_name)
  b_class_added=false
  case_contents_array.each do | case_content|
    if case_content[0]==class_name
      b_class_added=true
    end
    break if b_class_added==true
  end
  b_class_added
end

#order_the_features(tests, case_contents_array) ⇒ Object



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/tdrunner_monitor.rb', line 338

def order_the_features(tests,case_contents_array)
  ordered_tests=[]
  ordered_contents=[]
  tests_to_string=[]
  not_found_tests=[]
  tests.each do |tc|
    tests_to_string << tc.to_s
  end

  case_contents_array.each  do|tc|
    if not_found_tests.include?(tc[0].to_s)==false
      index=tests_to_string.index tc[0].to_s
      if index!=nil
        ordered_tests.push tests[index]
        ordered_contents.push tc
      else
        tests_to_be_added=tests.find_all{|item| item.to_s % item.to_s == tc[0].to_s }
        if tests_to_be_added!=[]
          ordered_tests.push tests_to_be_added.first
          ordered_contents.push tc
        else
          not_found_tests << tc[0].to_s
        end
      end
    end
  end

  return ordered_tests, ordered_contents
end

#order_the_tests(tests, case_contents_array) ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/tdrunner_monitor.rb', line 303

def order_the_tests(tests,case_contents_array)
  ordered_tests=[]
  ordered_contents=[]
  tests_to_string=[]
  not_found_tests=[]
  tests.each do |tc|
    tests_to_string << tc.to_s
  end
  test_classes=get_test_classes(tests)
  case_contents_array.each  do|tc|
    if not_found_tests.include?(tc[0].to_s)==false
      index=tests_to_string.index tc[0].to_s
      if index!=nil
        ordered_tests.push tests[index]
        ordered_contents.push tc
      else
        tests_to_be_added=tests.find_all{|item| item.to_s % item.to_s == tc[0].to_s }
        if tests_to_be_added!=[]
          ordered_tests.push tests_to_be_added.first
          ordered_contents.push tc
        else
          not_found_tests << tc[0].to_s
        end
      end
    end
  end
  test_classes.each do |test_class|
    if is_class_included(case_contents_array,test_class.to_s)==false
      ordered_tests.push test_class
      ordered_contents.push [test_class.to_s,nil,test_class.to_s,'not run']
    end
  end
  return ordered_tests, ordered_contents
end

#organize_features(tests, case_contents_array, case_name_array, tdrunner_parameters) ⇒ Object



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/tdrunner_monitor.rb', line 392

def organize_features(tests,case_contents_array,case_name_array,tdrunner_parameters)
  organized_tests=[]

  tests.each do |test_case|
    profile_index=case_name_array.index test_case.to_s
    if profile_index == nil
      File.open(tdrunner_parameters.execution_profile, 'a') { |file| file.write("\n"+test_case.to_s+"=1") } if tdrunner_parameters.execution_mode=='-m'
    else
      case_contents_array.each do |test|
        if test_case.to_s==test[0].to_s

          organized_tests.push test_case

        end
      end
    end

  end

  organized_tests
end

#organize_tests(tests, case_contents_array, case_name_array, tdrunner_parameters) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/tdrunner_monitor.rb', line 368

def organize_tests(tests,case_contents_array,case_name_array,tdrunner_parameters)
  organized_tests=[]
  test_classes=get_test_classes(tests)
  tests.each do |test_case|
    profile_index=case_name_array.index test_case.to_s
    if profile_index == nil
      File.open(tdrunner_parameters.execution_profile, 'a') { |file| file.write("\n"+test_case.to_s+"=1") } if tdrunner_parameters.execution_mode=='-m'
    else
      case_contents_array.each do |test|
        if test_case.to_s==test[0].to_s

          organized_tests.push test_case

        end
      end
    end

  end
  test_classes.each do |test_class|
    organized_tests.push test_class
  end
  organized_tests
end

#randomize_features(tests, tdrunner_parameters) ⇒ Object

method for randomizing the executed features



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/tdrunner_monitor.rb', line 425

def randomize_features(tests,tdrunner_parameters)
  tests=flatten_tests(tests)
  if tdrunner_parameters.execution_profile!=nil
    f = File.open(tdrunner_parameters.execution_profile) or die "Unable to open file..."
    @case_contents_array=[]   # start with an empty array
    case_name_array=[]
    b_profile_file_exist=false

    f.each_line do |line|
      case_arr=line.split('=')
      if case_arr.length == 2
        for i in (1..case_arr[1].to_i)
          @case_contents_array << [case_arr[0],nil,case_arr[0],'not run'] #name, parameters, place in tree, test result
          case_name_array << case_arr[0]
        end
        b_profile_file_exist=true
      elsif case_arr.length == 3
        for i in (1..case_arr[1].to_i)
          @case_contents_array << [case_arr[0],case_arr[2],case_arr[0],'not run'] #name, parameters, place in tree, test result
          case_name_array << case_arr[0]
        end
        b_profile_file_exist=true
      end
    end
    tests=organize_features(tests,@case_contents_array,case_name_array,tdrunner_parameters)
  end

  if tdrunner_parameters.tdrunner_ordered==false
    if b_profile_file_exist==true
      @case_contents_array=@case_contents_array.sort_by { rand }
      tests,@case_contents_array=order_the_features(tests, @case_contents_array)
    else
      tests=tests.sort_by { rand }
    end
  else
    if b_profile_file_exist==true
      tests,@case_contents_array=order_the_features(tests, @case_contents_array)
    end
  end
  return tests, @case_contents_array

end

#randomize_test_cases(tests, tdrunner_parameters) ⇒ Object

method for randomizing the executed tests



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/tdrunner_monitor.rb', line 468

def randomize_test_cases(tests,tdrunner_parameters)
  tests=flatten_tests(tests)
  if tdrunner_parameters.execution_profile!=nil
    f = File.open(tdrunner_parameters.execution_profile) or die "Unable to open file..."
    @case_contents_array=[]   # start with an empty array
    case_name_array=[]
    b_profile_file_exist=false

    f.each_line do |line|
      case_arr=line.split('=')
      if case_arr.length == 2
        for i in (1..case_arr[1].to_i)
          @case_contents_array << [case_arr[0].gsub('-',''),nil,case_arr[0],'not run'] #name, parameters, place in tree, test result
          case_name_array << case_arr[0].gsub('-','')
        end
        b_profile_file_exist=true
      elsif case_arr.length >= 3
        for i in (1..case_arr[1].to_i)
          arguments=''
          if case_arr.length > 3
            counter=0
            case_arr.each do |value|                  
              if counter>1
                if counter==case_arr.length-1
                  arguments << value
                else
                  arguments << value+'='
                end
              end
              counter+=1
            end
          else              
            arguments=case_arr[2]
          end
          @case_contents_array << [case_arr[0].gsub('-',''),arguments,case_arr[0],'not run'] #name, parameters, place in tree, test result
          case_name_array << case_arr[0].gsub('-','')
        end
        b_profile_file_exist=true
      end
    end
    tests=organize_tests(tests,@case_contents_array,case_name_array,tdrunner_parameters)

  end

  if tdrunner_parameters.tdrunner_ordered==false
    if b_profile_file_exist==true
      @case_contents_array=@case_contents_array.sort_by { rand }
      tests,@case_contents_array=order_the_tests(tests, @case_contents_array)
    else
      tests=tests.sort_by { rand }
    end
  else
    if b_profile_file_exist==true
      tests,@case_contents_array=order_the_tests(tests, @case_contents_array)
    end
  end
  return tests, @case_contents_array

end

#record_test_to_recorder_profile(framework, record) ⇒ Object



570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'lib/tdrunner_monitor.rb', line 570

def record_test_to_recorder_profile(framework,record)
  if $tdrunner_parameters.recorded_execution_profile
    $recorded_tests ||= Array.new
    case framework
    when 'cucumber'
      record.each do |feature|
        $recorded_tests << "#{feature}=1"
      end
    when 'test_unit'
      $recorded_tests << "#{record}=1"
    end
  end
end

#remove_extra_ends_from_implementation(found_implementation, ends_to_remove) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/tdrunner_monitor.rb', line 141

def remove_extra_ends_from_implementation(found_implementation, ends_to_remove)
  removed_ends=0
  current_index=found_implementation.length
  found_implementation.reverse_each do |reverse_line|
    current_index-=1
    if removed_ends < ends_to_remove
      if reverse_line.include?('end')
        found_implementation.delete_at(current_index)
        removed_ends+=1
      end
    end
  end
  found_implementation
end

#tdrunner_active(run_start_time, iteration, tdrunner_parameters) ⇒ Object

Method to check how long tdrunner run is active TDRunner mode is checked from initialized TDRunnerParameters class



530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'lib/tdrunner_monitor.rb', line 530

def tdrunner_active(run_start_time,iteration,tdrunner_parameters)
  current_run_time=Time.now-run_start_time
  case tdrunner_parameters.execution_mode
  when '-H'
    if (current_run_time<format_execution_duration(tdrunner_parameters.duration_value))
      true
    else
      false
    end
  when '-i'
    if iteration<tdrunner_parameters.lpt_run
      true
    else
      false
    end
  when '-d'
    if Time.now<tdrunner_parameters.lpt_run
      true
    else
      false
    end
  when '-m'
    #exit_tdrunner_execution('Execution profile created: '+ tdrunner_parameters.execution_profile)
    false
  when '-e'
    if iteration<tdrunner_parameters.lpt_run
      true
    else
      false
    end
  end
end

#trap_interruptObject



120
121
122
123
124
# File 'lib/tdrunner_monitor.rb', line 120

def trap_interrupt
  trap('INT') do
    trap_pause unless $tdrunner_paused
  end
end

#trap_pauseObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/tdrunner_monitor.rb', line 101

def trap_pause
  $tdrunner_paused=true
  puts "\nExecution paused...\n"
  puts "\nStop execution Yes or No?:"
  response=STDIN.gets
  if response.capitalize == "Yes\n" || response.capitalize == "Y\n"
    $tdrunner_interrupted=true
    STDERR.puts "\nFinalizing test... Please wait...\n"
    STDERR.puts "\nPress ctrl+Break to interrupt immediately\n"
    exit
  elsif response.capitalize == "No\n" || response.capitalize == "N\n"
    STDERR.puts "\nContinuing execution..."
  else
    STDERR.puts "\nIncorrect answer"
    trap_pause
  end
  $tdrunner_paused=nil
end

#value_exists_in_array(current_array, value) ⇒ Object



414
415
416
417
418
419
420
421
422
# File 'lib/tdrunner_monitor.rb', line 414

def value_exists_in_array(current_array,value)
  b_exists=false
  current_array.each do |arr_value|
    if arr_value.include?(value)
      b_exists=true
    end
  end
  b_exists
end

#wait_for_starttime(tdrunner_parameters) ⇒ Object



584
585
586
587
588
589
590
591
# File 'lib/tdrunner_monitor.rb', line 584

def wait_for_starttime(tdrunner_parameters)
  #If the start time is defined then only start execution at this time
  if tdrunner_parameters.start_time!=nil
    while Time.now < tdrunner_parameters.start_time
      sleep(1)
    end
  end
end

#write_failed_sip_file(tdrunner_parameters) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/tdrunner_monitor.rb', line 83

def write_failed_sip_file(tdrunner_parameters)
  if($failed_tests!=nil && tdrunner_parameters.failed_execution_profile != nil && !$failed_tests.empty?)
    file = File.open(tdrunner_parameters.failed_execution_profile.to_s,'w')
    $failed_tests.flatten.each { |x| file.write(x.to_s+"\n")}
    file.close
    puts 'Failed execution profile created: '+ tdrunner_parameters.failed_execution_profile
  end
end

#write_recorded_sip_file(tdrunner_parameters) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/tdrunner_monitor.rb', line 92

def write_recorded_sip_file(tdrunner_parameters)
  if($recorded_tests!=nil && tdrunner_parameters.recorded_execution_profile != nil && !$recorded_tests.empty?)
    file = File.open(tdrunner_parameters.recorded_execution_profile.to_s,'w')
    $recorded_tests.flatten.each { |x| file.write(x.to_s+"\n")}
    file.close
    puts 'Recorded execution profile created: '+ tdrunner_parameters.recorded_execution_profile
  end
end