Module: TestScore

Defined in:
lib/testscore.rb

Class Method Summary collapse

Class Method Details

.log_resultsObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/testscore.rb', line 63

def self.log_results
  puts "\n\nReporting test suite performance to TestScore."
  host = ENV.fetch('TESTSCORE_REPORT_HOST', 'https://testscore.io')
  uri = URI("#{host}/results/#{@api_key}")
  req = Net::HTTP::Post.new(
    uri,
    'Content-Type' => 'application/json',
    'Content-Encoding' => 'gzip',
  )
  gzip = Zlib::GzipWriter.new(StringIO.new)
  gzip << { examples: @results }.to_json
  req.body = gzip.close.string
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = (uri.scheme == "https")
  http.request(req)
rescue => e
  puts "Unable to report results to TestScore: #{e.message}"
end

.log_suite_startedObject



43
44
45
46
# File 'lib/testscore.rb', line 43

def self.log_suite_started
  @start_time = Time.now
  ActiveSupport::Notifications.subscribe('sql.active_record', ActiveRecord::QueryCounter.new)
end

.spec_ended(example) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/testscore.rb', line 54

def self.spec_ended(example)
  @results[example.id] = {
    duration: Time.now - @results[example.id][:started_at],
    description: example.description,
    metadata: example..slice(:line_number, :file_path),
    queries: ActiveRecord::QueryCounter.queries
  }
end

.spec_started(example) ⇒ Object



48
49
50
51
52
# File 'lib/testscore.rb', line 48

def self.spec_started(example)
  @results ||= {}
  @results[example.id] = { started_at: Time.now }
  ActiveRecord::QueryCounter.queries = []
end

.start(api_key) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/testscore.rb', line 82

def self.start(api_key)
  @api_key = api_key

  RSpec.configure do |config|
    config.before(:each) do |example|
      TestScore.spec_started(example)
    end

    config.after(:each) do |example|
      TestScore.spec_ended(example)
    end

    config.before(:suite) do |suite|
      TestScore.log_suite_started
    end

    config.after(:suite) do |suite|
      TestScore.log_results
    end
  end
end