Class: Toaster::TestCoverage

Inherits:
Object
  • Object
show all
Defined in:
lib/toaster/test/test_coverage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test_suite, state_graph = nil) ⇒ TestCoverage

Returns a new instance of TestCoverage.



17
18
19
20
21
22
23
24
# File 'lib/toaster/test/test_coverage.rb', line 17

def initialize(test_suite, state_graph = nil)
  @state_graph = state_graph
  @test_suite = test_suite
  if !state_graph
    automation = test_suite.automation
    @state_graph = StateTransitionGraph.build_graph_for_test_suite(@test_suite)
  end 
end

Instance Attribute Details

#state_graphObject

Returns the value of attribute state_graph.



15
16
17
# File 'lib/toaster/test/test_coverage.rb', line 15

def state_graph
  @state_graph
end

#test_suiteObject

Returns the value of attribute test_suite.



15
16
17
# File 'lib/toaster/test/test_coverage.rb', line 15

def test_suite
  @test_suite
end

Instance Method Details

#covered_statesObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/toaster/test/test_coverage.rb', line 67

def covered_states()
  result = Set.new
  executed_tests = @test_suite.test_cases
  @state_graph.nodes().each do |n|
    tests = tests_covering_state(n, true)
    if !tests.empty?()
      result << n
    end
  end
  return result
end

#covered_transitionsObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/toaster/test/test_coverage.rb', line 55

def covered_transitions()
  result = Set.new
  executed_tests = @test_suite.test_cases
  @state_graph.edges().each do |e|
    tests = tests_covering_transition(e, true)
    if !tests.empty?()
      result << e
    end
  end
  return result
end

#cvg_statesObject



26
27
28
# File 'lib/toaster/test/test_coverage.rb', line 26

def cvg_states()
  return covered_states().size.to_f / @state_graph.nodes().size.to_f
end

#test_executed?(test_case) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/toaster/test/test_coverage.rb', line 30

def test_executed?(test_case)
  executed_tests = @test_suite.test_cases
  skip_task_uuids = test_case.skip_task_uuids
  repeat_task_uuids = test_case.repeat_task_uuids
  attrs = test_case.test_attributes

  executed_tests.each do |t|
    # check skip_task_uuids
    if t.skip_task_uuids.eql?(skip_task_uuids)
      # check repeat_task_uuids
      if t.repeat_task_uuids.eql?(repeat_task_uuids)
        # check automation run attributes
        if t.automation_run
          attrs1 = t.automation_run.run_attributes
          if attrs1.eql?(attrs)
            puts "INFO: test case already executed: skip_tasks: #{skip_task_uuids}, repeat_tasks: #{repeat_task_uuids}, attributes: #{attrs}"
            return true
          end
        end
      end
    end
  end
  return false
end

#tests_covering_state(state_node, return_after_first_result = false) ⇒ Object



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
# File 'lib/toaster/test/test_coverage.rb', line 79

def tests_covering_state(state_node, return_after_first_result=false)
  result = Set.new
  executed_tests = @test_suite.test_cases
  pre_task = state_node.preceding_task
  suc_task = state_node.succeeding_task

  executed_tests.each do |test|
    # check if the post-state of the pre-task of the 
    # given state matches for this test execution
    if pre_task
      exec = test.task_execution(pre_task.uuid)
      if exec
        poststate = exec.state_after()
        if state_node.subset_of?(poststate, ["__task_num__"])
          result << test
          return result if return_after_first_result
          next
        end
      end
    end
    # check if the pre-state of the post-task of the 
    # given state matches for this test execution
    if suc_task
      exec = test.task_execution(suc_task.uuid)
      if exec
        prestate = exec.state_before
        if state_node.subset_of?(prestate, ["__task_num__"])
          result << test
          return result if return_after_first_result
        end
      end
    end
  end

  return result
end

#tests_covering_transition(transition_edge, return_after_first_result = false) ⇒ Object



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
# File 'lib/toaster/test/test_coverage.rb', line 116

def tests_covering_transition(transition_edge, return_after_first_result=false)
  result = Set.new
  task = transition_edge.represented_task
  executed_tests = @test_suite.test_cases
  executed_tests.each do |test|
    # check if task has been executed by test case
    if !test.executed_task_uuids().include?(task.uuid)
      next
    end
    exec = test.task_execution(task.uuid)

    # check if the task parameters match the transition condition
    params = exec.get_used_parameters()
    if !transition_edge.matches_parameters?(params)
      next
    end

    # check if the pre-state and post-state match
    prestate = exec.state_before()
    poststate = exec.state_after()
    if !transition_edge.matches_states?(prestate, poststate)
      next
    end

    # all tests passed, add to result
    result << test
    return result if return_after_first_result
  end
end