Class: AllureRubyApi::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/allure-ruby-api/builder.rb

Constant Summary collapse

MUTEX =
Mutex.new

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.suitesObject

Returns the value of attribute suites.



10
11
12
# File 'lib/allure-ruby-api/builder.rb', line 10

def suites
  @suites
end

Class Method Details

.add_attachment(suite, test, opts = {:step => nil, :file => nil, :mime_type => nil}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/allure-ruby-api/builder.rb', line 72

def add_attachment(suite, test, opts = {:step => nil, :file => nil, :mime_type => nil})
  raise "File cannot be nil!" if opts[:file].nil?
  step = opts[:step]
  file = opts[:file]
  title = opts[:title] || file.basename
  puts "Adding attachment #{opts[:title]} to #{suite}.#{test}#{step.nil? ? "" : ".#{step}"}"
  dir = Pathname.new(Dir.pwd).join(config.output_dir)
  FileUtils.mkdir_p(dir)
  file_extname = File.extname(file.path.downcase)
  mime_type = opts[:mime_type] || MimeMagic.by_path(file.path) || "text/plain"
  attachment = dir.join("#{Digest::SHA256.file(file.path).hexdigest}-attachment#{(file_extname.empty?) ? '' : file_extname}")
  FileUtils.cp(file.path, attachment)
  attach = {
      :type => mime_type,
      :title => title,
      :source => attachment.basename,
      :file => attachment.basename,
      :target => attachment.basename,
      :size => File.stat(attachment).size
  }
  if step.nil?
    self.suites[suite][:tests][test][:attachments] << attach
  else
    self.suites[suite][:tests][test][:steps][step][:attachments] << attach
  end
end

.build!(opts = {}, &block) ⇒ Object



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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/allure-ruby-api/builder.rb', line 115

def build!(opts = {}, &block)
  suites_xml = []
  self.suites.each do |suite_title, suite|
    builder = Nokogiri::XML::Builder.new do |xml|
      xml.send "ns2:test-suite", :start => suite[:start] || 0, :stop => suite[:stop] || 0, 'xmlns' => '', "xmlns:ns2" => "urn:model.allure.qatools.yandex.ru" do
        xml.send :name, suite_title
        xml.send :title, suite_title
        xml.send "test-cases" do
          suite[:tests].each do |test_title, test|
            xml.send "test-case", :start => test[:start] || 0, :stop => test[:stop] || 0, :status => test[:status] do
              xml.send :name, test_title
              xml.send :title, test_title
              unless test[:failure].nil?
                xml.failure do
                  xml.message test[:failure][:message]
                  xml.send "stack-trace", test[:failure][:stacktrace]
                end
              end
              xml.steps do
                test[:steps].each do |step_title, step_obj|
                  xml.step(:start => step_obj[:start] || 0, :stop => step_obj[:stop] || 0, :status => step_obj[:status]) do
                    xml.send :name, step_title
                    xml.send :title, step_title
                    xml_attachments(xml, step_obj[:attachments])
                  end
                end
              end
              xml_attachments(xml, test[:attachments])
              xml_labels(xml, suite[:labels].merge(test[:labels]))
              xml.parameters
            end
          end
        end
        xml_labels(xml, suite[:labels])
      end
    end
    xml = builder.to_xml
    xml = yield suite, xml if block_given?
    dir = Pathname.new(config.output_dir)
    FileUtils.mkdir_p(dir)
    out_file = dir.join("#{UUID.new.generate}-testsuite.xml")
    File.open(out_file, 'w+') do |file|
      file.write(validate_xml(xml))
    end
    suites_xml << xml
  end
  suites_xml
end

.start_step(suite, test, step) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/allure-ruby-api/builder.rb', line 61

def start_step(suite, test, step)
  MUTEX.synchronize do
    puts "Starting step #{suite}.#{test}.#{step}"
    self.suites[suite][:tests][test][:steps][step] = {
        :title => step,
        :start => timestamp,
        :attachments => []
    }
  end
end

.start_suite(suite, labels = {:severity => :normal}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/allure-ruby-api/builder.rb', line 13

def start_suite(suite, labels = {:severity => :normal})
  init_suites
  MUTEX.synchronize do
    puts "Starting case_or_suite #{suite} with labels #{labels}"
    self.suites[suite] = {
        :title => suite,
        :start => timestamp,
        :tests => {},
        :labels => labels
    }
  end
end

.start_test(suite, test, labels = {:severity => :normal}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/allure-ruby-api/builder.rb', line 26

def start_test(suite, test, labels = {:severity => :normal})
  MUTEX.synchronize do
    puts "Starting test #{suite}.#{test} with labels #{labels}"
    self.suites[suite][:tests][test] = {
        :title => test,
        :start => timestamp,
        :failure => nil,
        :steps => {},
        :attachments => [],
        :labels => labels,
    }
  end
end

.stop_step(suite, test, step, status = :passed) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/allure-ruby-api/builder.rb', line 99

def stop_step(suite, test, step, status = :passed)
  MUTEX.synchronize do
    puts "Stopping step #{suite}.#{test}.#{step}"
    self.suites[suite][:tests][test][:steps][step][:stop] = timestamp
    self.suites[suite][:tests][test][:steps][step][:status] = status
  end
end

.stop_suite(title) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/allure-ruby-api/builder.rb', line 107

def stop_suite(title)
  init_suites
  MUTEX.synchronize do
    puts "Stopping case_or_suite #{title}"
    self.suites[title][:stop] = timestamp
  end
end

.stop_test(suite, test, result = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/allure-ruby-api/builder.rb', line 40

def stop_test(suite, test, result = {})
  self.suites[suite][:tests][test][:steps].each do |step_title, step|
    if step[:stop].nil? || step[:stop] == 0
      stop_step(suite, test, step_title, result[:status])
    end
  end
  MUTEX.synchronize do
    puts "Stopping test #{suite}.#{test}"
    self.suites[suite][:tests][test][:stop] = timestamp(result[:finished_at])
    self.suites[suite][:tests][test][:start] = timestamp(result[:started_at])
    self.suites[suite][:tests][test][:status] = result[:status]
    if (result[:status].to_sym != :passed)
      self.suites[suite][:tests][test][:failure] = {
          :stacktrace => ((result[:exception] && result[:exception].backtrace) || []).map { |s| s.to_s }.join("\r\n"),
          :message => result[:exception].to_s,
      }
    end

  end
end