Class: Flaky::Run
Instance Attribute Summary collapse
-
#result_dir ⇒ Object
readonly
Returns the value of attribute result_dir.
-
#result_file ⇒ Object
readonly
Returns the value of attribute result_file.
-
#tests ⇒ Object
readonly
Returns the value of attribute tests.
Instance Method Summary collapse
- #_execute(run_cmd, test_name, runs, appium) ⇒ Object
- #execute(opts = {}) ⇒ Object
-
#initialize ⇒ Run
constructor
A new instance of Run.
- #report(opts = {}) ⇒ Object
Methods included from Color
Constructor Details
#initialize ⇒ Run
Returns a new instance of Run.
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/flaky/run.rb', line 39 def initialize @tests = {} @start_time = Time.now result_dir = '/tmp/flaky/' # rm -rf result_dir FileUtils.rm_rf result_dir FileUtils.mkdir_p result_dir @result_dir = result_dir @result_file = File.join result_dir, 'result.txt' end |
Instance Attribute Details
#result_dir ⇒ Object (readonly)
Returns the value of attribute result_dir.
37 38 39 |
# File 'lib/flaky/run.rb', line 37 def result_dir @result_dir end |
#result_file ⇒ Object (readonly)
Returns the value of attribute result_file.
37 38 39 |
# File 'lib/flaky/run.rb', line 37 def result_file @result_file end |
#tests ⇒ Object (readonly)
Returns the value of attribute tests.
37 38 39 |
# File 'lib/flaky/run.rb', line 37 def tests @tests end |
Instance Method Details
#_execute(run_cmd, test_name, runs, appium) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/flaky/run.rb', line 90 def _execute run_cmd, test_name, runs, appium # must capture exit code or log is an array. log, exit_code = Open3.capture2e run_cmd result = /\d+ runs, \d+ assertions, \d+ failures, \d+ errors, \d+ skips/ success = /0 failures, 0 errors, 0 skips/ passed = true found_results = log.scan result # all result instances must match success found_results.each do |result| # runs must be >= 1. 0 runs mean no tests were run. r_count = result.match /(\d+) runs/ runs_not_zero = r_count && r_count[1] && r_count[1].to_i > 0 ? true : false unless result.match(success) && runs_not_zero passed = false break end end # no results found. passed = false if found_results.length <= 0 pass_str = passed ? 'pass' : 'fail' test = @tests[test_name] # save log if passed pass = test[:pass] += 1 postfix = "pass_#{pass}" else fail = test[:fail] += 1 postfix = "fail_#{fail}" end postfix = "#{runs}_#{test_name}_" + postfix postfix = '0' + postfix if runs <= 9 log_file = LogArtifact.new result_dir: result_dir, pass_str: pass_str, test_name: test_name # html Ruby test log Flaky.write log_file.name("#{postfix}.html"), log # TODO: Get iOS simulator system log from appium # File.open(log_file.name("#{postfix}.server.log.txt"), 'w') do |f| # f.write appium.tail.out.readpartial(999_999_999) # end # adb logcat log logcat = appium.logcat ? appium.logcat.stop : nil File.open(log_file.name("#{postfix}.logcat.txt"), 'w') do |f| f.write logcat end if logcat # appium server log Flaky.write log_file.name("#{postfix}.appium.html"), nil, appium.flush_buffer passed end |
#execute(opts = {}) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/flaky/run.rb', line 149 def execute opts={} run_cmd = opts[:run_cmd] test_name = opts[:test_name] appium = opts[:appium] raise 'must pass :run_cmd' unless run_cmd raise 'must pass :test_name' unless test_name raise 'must pass :appium' unless appium test = @tests[test_name] ||= {runs: 0, pass: 0, fail: 0} runs = test[:runs] += 1 passed = _execute run_cmd, test_name, runs, appium print cyan("\n #{test_name} ") if @last_test.nil? || @last_test != test_name print passed ? green(' ✓') : red(' ✖') @last_test = test_name passed end |
#report(opts = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/flaky/run.rb', line 52 def report opts={} save_file = opts.fetch :save_file, true puts "\n" * 2 success = '' failure = '' total_success = 0 total_failure = 0 @tests.each do |name, stats| runs = stats[:runs] pass = stats[:pass] fail = stats[:fail] line = "#{name}, runs: #{runs}, pass: #{pass}," + " fail: #{fail}\n" if fail > 0 && pass <= 0 failure += line total_failure += 1 else success += line total_success += 1 end end out = "#{total_success + total_failure} Tests\n\n" out += "Failure (#{total_failure}):\n#{failure}\n" unless failure.empty? out += "Success (#{total_success}):\n#{success}" unless success.empty? duration = Time.now - @start_time duration = ChronicDuration.output(duration.round) || '0s' out += "\nFinished in #{duration}" # overwrite file File.open(@result_file, 'w') do |f| f.puts out end if save_file puts out end |