Class: CTT::Cli::Command::MultipleTests

Inherits:
Base show all
Includes:
CTT::Cli
Defined in:
lib/cli/commands/multiple_tests.rb

Constant Summary collapse

MAX_RERUN_TIMES =
10

Constants included from CTT::Cli

RESULTS_SERVER_URL, STATIC_COMMANDS, TEST_RESULT_FILE, TEST_SUITE_CONFIG_FILE, VERSION

Instance Method Summary collapse

Constructor Details

#initialize(command, args, runner) ⇒ MultipleTests

Returns a new instance of MultipleTests.



10
11
12
13
14
# File 'lib/cli/commands/multiple_tests.rb', line 10

def initialize(command, args, runner)
  super(args, runner)
  @suites = @runner.suites
  @command = command
end

Instance Method Details

#format_time(t) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/cli/commands/multiple_tests.rb', line 114

def format_time(t)
  time_str = ''
  time_str += (t / 3600).to_i.to_s + " hours " if t > 3600
  time_str += (t % 3600 / 60).to_i.to_s + " minutes " if t > 60
  time_str += (t % 60).to_f.round(2).to_s + " seconds"
  time_str
end

#get_rerun_folder(result_folder) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/cli/commands/multiple_tests.rb', line 122

def get_rerun_folder(result_folder)
  rerun_folder = result_folder
  i = MAX_RERUN_TIMES
  while(i > 0)
    if File.exists?(File.join(result_folder, "rerun#{i}"))
      rerun_folder = File.join(result_folder, "rerun#{i}")
      break
    end
    i -= 1
  end
  rerun_folder
end


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

def print_cases_summary(summary)
  say("\nFinished in #{format_time(summary[:duration])}")

  color = :green
  color = :yellow if summary[:pending] > 0
  color = :red if summary[:failed] > 0
  say("#{summary[:total]} examples, #{summary[:failed]} failures, #{summary[:pending]} pendings", color)
end


101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/cli/commands/multiple_tests.rb', line 101

def print_failed_cases(summary)
  unless summary[:failed_cases].empty?
    say("\nFailures:")
    summary[:failed_cases].each do |suite, cases|
      say("  Test Suite: #{suite}", :yellow)
      cases.each do |c|
        say("    #{c.strip}", :red)
      end
    end
    say("execute #{yellow("rerun")} command to run all failed cases.")
  end
end

#rerunObject



26
27
28
29
30
# File 'lib/cli/commands/multiple_tests.rb', line 26

def rerun
  say("rerun failed cases for multiple test suites", :green)
  rerun_tests
  show_summary(true)
end

#rerun_testsObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/cli/commands/multiple_tests.rb', line 32

def rerun_tests
  index = 1
  @suites.suites["suites"].each do |name, _|
    say("#{index}) start to run failed cases for test suite: #{name}\n", :yellow)
    args = @args.insert(0, @command)
    cmd = TestSuite.new(name, args, @runner)
    cmd.run
    index += 1
  end
end

#runObject



16
17
18
# File 'lib/cli/commands/multiple_tests.rb', line 16

def run
  eval(@command)
end

#run_testsObject



43
44
45
46
47
48
49
50
51
# File 'lib/cli/commands/multiple_tests.rb', line 43

def run_tests
  index = 1
  @suites.suites["suites"].each do |name, _|
    say("#{index}) start to run test suite: #{name}\n", :yellow)
    cmd = TestSuite.new(name, @args, @runner)
    cmd.run
    index += 1
  end
end

#show_summary(rerun = false) ⇒ Object



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
89
90
# File 'lib/cli/commands/multiple_tests.rb', line 53

def show_summary(rerun = false)
  summary = {:total => 0,
             :failed => 0,
             :pending => 0,
             :duration => 0.0,
             :failed_cases => {},
             :pending_cases => {}
  }
  @suites.suites["suites"].each do |name, path|
    suite_config_path = File.absolute_path(File.join(path, TEST_SUITE_CONFIG_FILE))
    suite_config = YAML.load_file(suite_config_path)
    unless suite_config["results"]
      say("no results field in #{suite_config_path}. abort!", :red)
      exit(1)
    end

    result_path   = File.join(path, suite_config["results"])
    if rerun
      result_file = File.join(get_rerun_folder(result_path), TEST_RESULT_FILE)
    else
      result_file   = File.join(result_path, TEST_RESULT_FILE)
    end

    report        = TestReport.new(result_file)
    report.parse

    summary[:total]         += report.summary[:total]
    summary[:failed]        += report.summary[:failed]
    summary[:pending]       += report.summary[:pending]
    summary[:duration]      += report.summary[:duration]
    summary[:failed_cases][name]    = report.summary[:failed_cases]
    summary[:pending_cases][name]   = report.summary[:pending_cases]
  end

  print_cases_summary(summary)
  print_failed_cases(summary)

end

#testsObject



20
21
22
23
24
# File 'lib/cli/commands/multiple_tests.rb', line 20

def tests
  say("run multiple test suites", :green)
  run_tests
  show_summary
end