Class: CfHighlander::Tests

Inherits:
Object
  • Object
show all
Defined in:
lib/cfhighlander.tests.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(component_name, options) ⇒ Tests

Returns a new instance of Tests.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cfhighlander.tests.rb', line 15

def initialize(component_name,options)
  @component_name = component_name
  @tests_dir = options[:directory]
  @test_files = options[:tests] || Dir["#{@tests_dir}/*.test.yaml"]
  @report_path = "reports"
  @debug = false
  @cases = []
  @report = []
  @time = nil
  @timestamp = nil
  @exit_code = 0
end

Instance Attribute Details

#casesObject

Returns the value of attribute cases.



8
9
10
# File 'lib/cfhighlander.tests.rb', line 8

def cases
  @cases
end

#exit_codeObject

Returns the value of attribute exit_code.



8
9
10
# File 'lib/cfhighlander.tests.rb', line 8

def exit_code
  @exit_code
end

#reportObject

Returns the value of attribute report.



8
9
10
# File 'lib/cfhighlander.tests.rb', line 8

def report
  @report
end

#report_pathObject

Returns the value of attribute report_path.



8
9
10
# File 'lib/cfhighlander.tests.rb', line 8

def report_path
  @report_path
end

#timeObject

Returns the value of attribute time.



8
9
10
# File 'lib/cfhighlander.tests.rb', line 8

def time
  @time
end

#timestampObject

Returns the value of attribute timestamp.



8
9
10
# File 'lib/cfhighlander.tests.rb', line 8

def timestamp
  @timestamp
end

Instance Method Details

#generate_report(type) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/cfhighlander.tests.rb', line 84

def generate_report(type)
  report_dir
  failures = @report.select { |k,v| k[:message] }
  report = {}
  report[:component] = @component_name
  report[:tests] = cases.size.to_s
  report[:pass] = (cases.size - failures.size).to_s
  report[:failures] = failures.size.to_s
  report[:time] = @time.to_s
  report[:timestamp] = @timestamp.to_s
  report[:testcases] = @report
  case type
  when 'json'
    report_json(report)
  when 'xml'
    report_xml(report)
  end
end

#get_casesObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cfhighlander.tests.rb', line 28

def get_cases
  @test_files.each do |file|
    test_case = load_test_case(file)
    test_parameters = test_case['test_parameters'] || {}
    @cases << { 
      metadata: test_case['test_metadata'],
      test_parameters: test_parameters,
      file: file,
      config: load_default_config.deep_merge(test_case)
    }
  end
end

#load_default_configObject



41
42
43
44
45
46
47
# File 'lib/cfhighlander.tests.rb', line 41

def load_default_config
  begin
    YAML.load_file("#{@component_name}.config.yaml", aliases: true) || {}
  rescue Errno::ENOENT => e
    {}
  end
end

#load_test_case(file) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/cfhighlander.tests.rb', line 49

def load_test_case(file)
  begin
    YAML.load_file(file, aliases: true)
  rescue Errno::ENOENT => e
    abort "No test file found for #{file}"
  end
end


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cfhighlander.tests.rb', line 57

def print_results
  failures = @report.select { |k,v| k[:failure] }
  puts "\n\s\s============================"
  puts "\s\s#    CfHighlander Tests    #"
  puts "\s\s============================\n\n"
  puts "\s\sPass: #{cases.size - failures.size}"
  puts "\s\sFail: #{failures.size}"
  puts "\s\sTime: #{@time}\n\n"

  if failures.any?
    @exit_code = 1
    puts "\s\s=========Failures========="
    failures.each do |failure|
      puts "\s\sName: #{failure[:name]}"
      puts "\s\sTest: #{failure[:test]}"
      puts "\s\sType: #{failure[:type]}"
      puts "\s\sMessage: #{failure[:failure][:message]}"
      puts "\s\s-------------------------"
    end
  end
end

#report_dirObject



80
81
82
# File 'lib/cfhighlander.tests.rb', line 80

def report_dir
  FileUtils.mkdir_p(@report_path) unless Dir.exist?(@report_path)
end

#report_json(report) ⇒ Object



122
123
124
125
126
# File 'lib/cfhighlander.tests.rb', line 122

def report_json(report)
  File.open("reports/report.json","w") do |f|
    f.write(JSON.pretty_generate(report))
  end
end

#report_xml(report) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cfhighlander.tests.rb', line 103

def report_xml(report)
  testsuite = report.map { |k,v| "#{k}=\"#{v}\"" if k != :testcases }
  File.open("reports/report.xml","w") do |f|
    f.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
    f.write("<testsuite #{testsuite.join(' ')}>\n")
    report[:testcases].each do |test|
      testcase = test.map { |k,v| "#{k}=\"#{v}\"" if k != :failure }
      if test[:failure]
        f.write("\t<testcase #{testcase.join(' ')}>\n")
        f.write("\t\t<failure message=\"#{test[:failure][:message]}\" type=\"#{test[:failure][:type]}\"/>\n")
        f.write("\t</testcase>\n")
      else
        f.write("\t<testcase #{testcase.join(' ')}/>\n")
      end
    end
    f.write("</testsuite>")
  end
end