sc1789
sc1789

Reputation: 86

How to convert test report log files into Jenkins Junit XML File format?

I've been assigned a task to do with Ruby, but as I'm new to it I'm searching over the internet for a proper solution to solve it:

  1. I have a list of log files reports in a web page actually divided in passed or failed.
  2. I need to take log files and convert them into a format compatible with Jenkins Junit XML File format.
  3. Everything, the passage from log to XML has to be written in Ruby.

Any ideas?!

I looked all over the internet and found information but still have no clear ideas how to solve this.

Are there any Ruby libraries that make this easier? Has anyone ever handled anything like this?

Upvotes: 2

Views: 4067

Answers (2)

Joniale
Joniale

Reputation: 595

Under this link there is a small explanation that may help https://pzolee.blogs.balabit.com/2012/11/jenkins-vs-junit-xml-format/

Basically you just use https://wiki.jenkins-ci.org/display/JENKINS/xUnit+Plugin to import a XML that follows the Junit schema. You can convert XML to Junit format having the schemes. A good description of the Junit format can be found under: llg.cubic.org/docs/junit/

Upvotes: 1

the Tin Man
the Tin Man

Reputation: 160601

You don't show the format you need, and I don't know what Jenkins needs, but creating XML is easy. Unfortunately, what you want will take a book, or several articles, which is beyond the scope of Stack Overflow. Basically though...

You can use a templating system, like ERB where you create templates for your overall XML document, or Nokogiri::Builder can be used to generate XML, or you can do it old school and use simple string interpolation to create your XML.

A syslog file is typically fairly well structured, at least for the first several fields, followed by free-form text which is the output of various commands. A log file from Apache is similar, with columns of text, followed by some free-form, but easily parsable text. There are gems here and there, along with tutorials on how to parse a log, so search around and you'll find something. The idea is you want to break down each line read into text you can assign to an XML node.

Once you have your fields, you can substitute them into the template or have Ruby interpolate the variables into strings, or use Builder to add the text between the tags.

It's not really hard, but is going to take several small tasks to accomplish.

Using string interpolation, if you wanted XML like:

<xml>
  <tag1>
    <tag2>some text</tag2>
    <tag2>some more text</tag2>
  </tag1>
</xml>

You could create it like:

var1 = "some text"
var2 = "some more text"
xml = %Q{
<xml>
  <tag1>
    <tag2>#{var1}</tag2>
    <tag2>#{var2}</tag2>
  </tag1>
</xml>
}
puts xml

Similarly, if you want to use ERB:

require 'erb'

var1 = "some text"
var2 = "some more text"

template = ERB.new <<-EOF
<xml>
  <tag1>
    <tag2><%= var1 %></tag2>
    <tag2><%= var2 %></tag2>
  </tag1>
</xml>
EOF
puts template.result(binding)

Which outputs:

<xml>
  <tag1>
    <tag2>some text</tag2>
    <tag2>some more text</tag2>
  </tag1>
</xml>

Or, using Nokogiri::Builder:

require 'nokogiri'

var1 = "some text"
var2 = "some more text"
builder = Nokogiri::XML::Builder.new do |node|
  node.xml {
    node.tag1 {
      [var1, var2].each do |t|
        node.tag2(t)
      end
    }
  }
end
puts builder.to_xml

Which outputs:

<?xml version="1.0"?>
<xml>
  <tag1>
    <tag2>some text</tag2>
    <tag2>some more text</tag2>
  </tag1>
</xml>

Upvotes: 1

Related Questions