Class: Res::Reporters::TestRail

Inherits:
Object
  • Object
show all
Defined in:
lib/res/reporters/test_rail.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ TestRail

Returns a new instance of TestRail.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/res/reporters/test_rail.rb', line 11

def initialize(args)
  @url = args[:url]
  @config = Res::Config.new([:user, :password, :namespace, :project, :suite], :optional => [:plan_name, :run_id, :run_name], :pre_env => 'testrail_')
  config.process(args)

  @case_status = {}
  @io = File.new("./.test_rail_reporter.log","w+") 

  test_rail_project = config.project
  @suite_name = config.suite 
  
 begin
    @project = tr.find_project(:name => test_rail_project)
  rescue
    @io.puts "Project: #{test_rail_project} could not be found" 
    @io.puts "  Please create a project first to contain test suite #{@suite_name}"
    @io.close
    return "Project #{test_rail_project} could not be found" 
  end
end

Instance Attribute Details

#case_statusObject

Returns the value of attribute case_status.



10
11
12
# File 'lib/res/reporters/test_rail.rb', line 10

def case_status
  @case_status
end

#configObject

Returns the value of attribute config.



10
11
12
# File 'lib/res/reporters/test_rail.rb', line 10

def config
  @config
end

#irObject

Returns the value of attribute ir.



10
11
12
# File 'lib/res/reporters/test_rail.rb', line 10

def ir
  @ir
end

#projectObject

Returns the value of attribute project.



10
11
12
# File 'lib/res/reporters/test_rail.rb', line 10

def project
  @project
end

#suiteObject

Returns the value of attribute suite.



10
11
12
# File 'lib/res/reporters/test_rail.rb', line 10

def suite
  @suite
end

Instance Method Details

#add_case_status(run_id) ⇒ Object

Add status to each testcase



117
118
119
120
121
# File 'lib/res/reporters/test_rail.rb', line 117

def add_case_status(run_id)
  @case_status.each { |case_id, status|
    @tr.add_result_for_case(:run_id => run_id, :case_id => case_id, :status => status)
  }
end

#case_details(ir, section) ⇒ Object

create_suite



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/res/reporters/test_rail.rb', line 141

def case_details(ir, section)
  ir[:children].each do |child|
    if @mappings.context.include?(child[:type])
        section = section.find_section(:name => child[:name])   
        case_details(child, section)

    elsif @mappings.case.include?(child[:type])  
        tcase = section.find_test_case(:title => child[:name])  if !section.nil?
        @case_status[:"#{tcase.id}"] = child[:status] if !tcase.nil?

    end
  end
end

#create_suite(ir, project_id, suite, parent) ⇒ Object

Add section or cases within parent section



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/res/reporters/test_rail.rb', line 124

def create_suite(ir, project_id, suite, parent)
  ir[:children].each do |child|
    if @mappings.context.include?(child[:type])
      section = parent.find_or_create_section(:project_id => project_id, :suite_id => suite.id, :name => child[:name])   
      create_suite(child, project_id, suite, section) if child[:children].count > 0

    elsif @mappings.case.include?(child[:type])
      parent = suite if parent.nil?   
      tcase = parent.find_or_create_test_case(:title => child[:name])
      @case_status[:"#{tcase.id}"] = child[:status]

    else
      # To be added. Ex: steps 
    end # if
  end # each
end

#submit_results(ir, args = {}) ⇒ Object

Submits run against suite Either creates a new run using run_name or use existing run_id



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
# File 'lib/res/reporters/test_rail.rb', line 52

def submit_results(ir, args = {})
  sync_tests(ir) if !@synced
  suite = @project.find_suite(:name => @suite_name)

  plan_name = @config.plan_name || args[:plan_name] || nil
  run_name = @config.run_name || args[:run_name] || nil
  run_id = @config.run_id || args[:run_id] || nil
  
  if !run_name.nil? 
    run_name = @config.run_name 
    @io.puts "> Created new run with name #{run_name}"

    if plan_name
      plan  = @project.find_or_create_plan(:name => plan_name, :id => @project.id)
      run_id = @tr.add_run_in_plan(:project_id => @project.id, :plan_id => plan.id, :name => run_name, :description => args[:run_description], :suite_id => suite.id).id
    else
      run_id = @tr.add_run( :project_id => @project.id, :name => run_name, :description => args[:run_description], :suite_id => suite.id ).id
    end
    @io.puts "> Created new run: #{run_id}"
  
  elsif !run_id.nil? 
    run_id = @config.run_id 
      begin
        run = @tr.get_run(:run_id => @config.run_id) 
        @io.puts "> Found run with id #{run_id}"
      rescue
        @io.puts "> Couldn't find run with id #{run_id}"
        @io.close
        return "Couldn't find run with id #{run_id}"           
      end

  else
    @io.puts "> run_name and run_id are either nil or not specified"
    @io.close
    return "run_name and run_id are either nil or not specified"          

  end

  i = 0
  if @case_status.empty?
    while i < @ir.results.count
      begin
        section = suite.find_section(:name => @ir.results[i][:name])
      rescue 
        @io.puts "> Couldn't find section with name #{@ir.results[i][:name]}" 
        puts "Couldn't find section with name #{@ir.results[i][:name]}"
      end
      case_details(@ir.results[i], section)
      i += 1
    end # while
  end # ifa
  add_case_status(run_id)
  @io.puts "> Added the test case status"
  @io.puts "> Submit Successful"
  @io.close
  return "Submitted to Test Rail"
end

#sync_tests(ir) ⇒ Object

Creates a new suite within testrail



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/res/reporters/test_rail.rb', line 33

def sync_tests(ir)
  @ir = ir
  @mappings = Res::Mappings.new(@ir.type)

  suite = @project.find_or_create_suite(:name => @suite_name, :id => @project.id)
  @io.puts "Syncing Suite"

  i = 0
    while i < @ir.results.count         
      section = suite.find_or_create_section(:project_id => @project.id, :suite_id => suite.id, :name => @ir.results[i][:name])   
      create_suite(@ir.results[i], @project.id, suite, section)
      i += 1
    end # while
  @synced = true
  @io.puts "> Sync Successful"
end

#trObject



110
111
112
113
114
# File 'lib/res/reporters/test_rail.rb', line 110

def tr
  @tr = ::TestRail::API.new( :user   => @config.user,
                          :password  => @config.password,
                          :namespace => @config.namespace) 
end