Class: Spectre::Reporter::VSTest

Inherits:
Object
  • Object
show all
Defined in:
lib/spectre/reporter/vstest.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ VSTest

Returns a new instance of VSTest.



12
13
14
15
# File 'lib/spectre/reporter/vstest.rb', line 12

def initialize config
  @config = config
  @date_format = '%FT%T.%L'
end

Instance Method Details

#report(run_infos) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/spectre/reporter/vstest.rb', line 17

def report run_infos
  now = Time.now.getutc

  xml_str = '<?xml version="1.0" encoding="UTF-8" ?>'
  xml_str += %{<TestRun xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">}

  started = run_infos[0].started
  finished = run_infos[-1].finished

  computer_name = Socket.gethostname

  xml_str += %{<Times start="#{started.strftime(@date_format)}" finish="#{finished.strftime(@date_format)}" />}


  # Write summary with file attachments
  xml_str += '<ResultSummary>'
  xml_str += '<ResultFiles>'
  xml_str += %{<ResultFile path="#{File.absolute_path(@config['log_file'])}"></ResultFile>} if File.exists? @config['log_file']

  report_files = Dir[File.join(@config['out_path'], '*')]

  if report_files.any?
    report_files.each do |report_file|
      xml_str += %{<ResultFile path="#{File.absolute_path(report_file)}"></ResultFile>}
    end
  end

  xml_str += '</ResultFiles>'
  xml_str += '</ResultSummary>'


  # Write test definitions
  test_definitions = run_infos
    .sort_by { |x| x.spec.name }
    .map { |x| [SecureRandom.uuid(), SecureRandom.uuid(), x] }

  xml_str += '<TestDefinitions>'
  test_definitions.each do |test_id, execution_id, run_info|
    xml_str += %{<UnitTest name="#{CGI::escapeHTML get_name(run_info)}" storage="#{CGI::escapeHTML(run_info.spec.file.to_s)}" id="#{test_id}">}
    xml_str += %{<Execution id="#{execution_id}" />}
    xml_str += '</UnitTest>'
  end
  xml_str += '</TestDefinitions>'


  # Write test results
  xml_str += '<Results>'
  test_definitions.each do |test_id, execution_id, run_info|
    duration_str = Time.at(run_info.duration).gmtime.strftime('%T.%L')

    if run_info.failed?
      outcome = 'Failed'
    elsif run_info.error?
      outcome = 'Failed'
    elsif run_info.skipped?
      outcome = 'Skipped'
    else
      outcome = 'Passed'
    end

    xml_str += %{<UnitTestResult executionId="#{execution_id}" testId="#{test_id}" testName="#{CGI::escapeHTML get_name(run_info)}" computerName="#{computer_name}" duration="#{duration_str}" startTime="#{run_info.started.strftime(@date_format)}" endTime="#{run_info.finished.strftime(@date_format)}" outcome="#{outcome}">}

    if run_info.log.any? or run_info.failed? or run_info.error?
      xml_str += '<Output>'

      # Write log entries
      xml_str += '<StdOut>'
      log_str = ''

      if run_info.properties.count > 0
        run_info.properties.each do |key, val|
          log_str += "#{key}: #{val}\n"
        end
      end

      if run_info.data
        data_str = run_info.data
        data_str = run_info.data.to_json unless run_info.data.is_a? String or run_info.data.is_a? Integer
        log_str += "data: #{data_str}\n"
      end

      run_info.log.each do |timestamp, message, level, name|
        log_text = ""
        begin
          log_text = message.dup.to_s
            .force_encoding("ISO-8859-1")
            .encode!("UTF-8")
        rescue 
          puts "ERROR in VSTEST - see message : #{message}"
        end

        log_str += %{#{timestamp.strftime(@date_format)} #{level.to_s.upcase} -- #{name}: #{CGI::escapeHTML(log_text)}\n}
      end

      xml_str += log_str
      xml_str += '</StdOut>'

      # Write error information
      if run_info.failed? or run_info.error?
        xml_str += '<ErrorInfo>'

        if run_info.failed? and not run_info.failure.cause
          xml_str += '<Message>'

          failure_message = "Expected #{run_info.failure.expectation}"
          failure_message += " with #{run_info.data}" if run_info.data
          failure_message += " but it failed"
          failure_message += " with message: #{run_info.failure.message}" if run_info.failure.message

          xml_str += CGI::escapeHTML(failure_message)

          xml_str += '</Message>'
        end

        if run_info.error or (run_info.failed? and run_info.failure.cause)
          error = run_info.error || run_info.failure.cause

          failure_message = error.message

          xml_str += '<Message>'
          xml_str += CGI::escapeHTML(failure_message)
          xml_str += '</Message>'

          stack_trace = error.backtrace.join "\n"

          xml_str += '<StackTrace>'
          xml_str += CGI::escapeHTML(stack_trace)
          xml_str += '</StackTrace>'
        end

        xml_str += '</ErrorInfo>'
      end

      xml_str += '</Output>'
    end


    xml_str += '</UnitTestResult>'
  end
  xml_str += '</Results>'


  # End report
  xml_str += '</TestRun>'


  Dir.mkdir(@config['out_path']) unless Dir.exists? @config['out_path']

  file_path = File.join(@config['out_path'], "spectre-vstest_#{now.strftime('%s')}.trx")

  File.write(file_path, xml_str)
end