Class: JayAPI::RSpec::TestDataCollector

Inherits:
Object
  • Object
show all
Includes:
Elasticsearch::Time, Git
Defined in:
lib/jay_api/rspec/test_data_collector.rb

Overview

Collects RSpec Test Data and pushes it to Jay’s Elasticsearch backend.

Constant Summary collapse

DEFAULT_BATCH_SIZE =
100
REQUIREMENTS_KEYS =

Meta-dara keys that can be used to annotate requirements in tests.

i[requirements refs].freeze

Constants included from Elasticsearch::Time

Elasticsearch::Time::TIME_FORMAT

Instance Method Summary collapse

Methods included from Elasticsearch::Time

#format_time

Constructor Details

#initialize(_output) ⇒ TestDataCollector

Executed by RSpec during initialization. Sets the @push_enabled instance variable according to configuration.



32
33
34
# File 'lib/jay_api/rspec/test_data_collector.rb', line 32

def initialize(_output)
  @push_enabled = configuration.push_enabled # When the push is disabled the class behaviour is inhibited.
end

Instance Method Details

#close(_notification) ⇒ Object

Executed by RSpec at the end of the test run. If the push is enabled, any test cases still being held in the Elasticsearch buffer are flushed.



87
88
89
90
91
# File 'lib/jay_api/rspec/test_data_collector.rb', line 87

def close(_notification)
  return unless push_enabled

  elasticsearch_index.flush
end

#example_finished(notification) ⇒ Object

Called by RSpec when an example is finished. The method extracts all the information from the given notification and creates a data Hash to push to Elasticsearch.

Parameters:

  • notification (RSpec::Core::Notifications::ExampleNotification)

    The Notification object passed by RSpec.



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
# File 'lib/jay_api/rspec/test_data_collector.rb', line 51

def example_finished(notification)
  return unless push_enabled

  example = notification.example
  identifier = example.full_description
  ex_result = example.execution_result
   = example.

  data = {
    test_env: {
      build_number: ENV['BUILD_NUMBER'],
      build_job_name: ENV['JOB_NAME'],
      repository: git_remote.presence,
      revision: git_revision.presence,
      hostname: hostname
    },
    test_case: {
      name: identifier,
      started_at: format_time(ex_result.started_at),
      finished_at: format_time(ex_result.finished_at),
      runtime: ex_result.run_time,
      id_long: identifier,
      id: build_short_id(identifier),
      location: [:location],
      requirements: requirements_from(),
      expectation: example.description,
      result: translate_result(ex_result.status),
      exception: ex_result.exception&.message&.strip
    }
  }

  elasticsearch_index.push(data)
end

#start(_notification) ⇒ Object

Called by RSpec at the beginning of the test run. If the push is enabled the method initializes the Elasticsearch::Index instance. This is done so that, if it cannot be initialized the tests will fail right away and not after execution.



40
41
42
43
44
# File 'lib/jay_api/rspec/test_data_collector.rb', line 40

def start(_notification)
  return unless push_enabled

  elasticsearch_index
end