Skip to content

Commit e9e5571

Browse files
authored
Merge pull request #338 from ahx/fractional-coverage
Add fractional digits in coverage result
2 parents c7867e4 + bec31b2 commit e9e5571

5 files changed

Lines changed: 18 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- OpenapiFirst::Test.report_coverage now includes fractional digits when returning a coverage value to avoid reporting "0% / no requests made" even though some requests have been made.
6+
57
## 2.4.0
68

79
- Support less verbose test setup without the need to call `OpenapiFirst::Test.report_coverage`, which will be called `at_exit`:

lib/openapi_first/test.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,30 @@ def self.minitest?(base)
1616
class Setup
1717
def initialize
1818
@minimum_coverage = 0
19+
@coverage_formatter = Coverage::TerminalFormatter
20+
@coverage_formatter_options = {}
1921
yield self
2022
end
2123

2224
def register(*)
2325
Test.register(*)
2426
end
2527

26-
attr_accessor :minimum_coverage
28+
def skip_response_coverage_for_status(*statuses); end
29+
30+
attr_accessor :minimum_coverage, :coverage_formatter_options, :coverage_formatter
2731

2832
# This called at_exit
2933
def handle_exit
3034
coverage = Coverage.result.coverage
3135
# :nocov:
3236
puts 'API Coverage did not detect any API requests for the registered API descriptions' if coverage.zero?
33-
Test.report_coverage if coverage.positive?
37+
if coverage.positive?
38+
Test.report_coverage(
39+
formatter: coverage_formatter,
40+
**coverage_formatter_options
41+
)
42+
end
3443
return unless minimum_coverage > coverage
3544

3645
puts "API Coverage fails with exit 2, because API coverage of #{coverage}%" \

lib/openapi_first/test/coverage/plan.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def coverage
4545
return 0 if done.zero?
4646

4747
all = tasks.count
48-
(done / (all.to_f / 100)).to_i
48+
(done / (all.to_f / 100))
4949
end
5050

5151
def tasks

spec/test/coverage/plan_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104

105105
plan.track_request(valid_request)
106106
plan.track_response(valid_response)
107-
expect(plan.coverage).to eq(66)
107+
expect(plan.coverage).to eq(66.66666666666667)
108108
plan.track_response(valid_400_response)
109109

110110
expect(plan.coverage).to eq(100)

spec/test_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
# Response not tracked
8383

8484
described_class.report_coverage
85-
expect(output.string).to include('The overal API validation coverage of this run is: 50%')
85+
expect(output.string).to include('The overal API validation coverage of this run is: 50.0%')
8686
end
8787

8888
it 'reports 100% if all requests/responses have been tracked' do
@@ -95,7 +95,7 @@
9595
definition.validate_response(valid_request, response, raise_error: true)
9696

9797
described_class.report_coverage
98-
expect(output.string).to include('The overal API validation coverage of this run is: 100%')
98+
expect(output.string).to include('The overal API validation coverage of this run is: 100.0%')
9999
end
100100

101101
context 'when passing verbose: true' do
@@ -113,7 +113,7 @@
113113
expected_output = [
114114
'✓ POST /roll',
115115
' ✓ 200(application/json)',
116-
'The overal API validation coverage of this run is: 100%'
116+
'The overal API validation coverage of this run is: 100.0%'
117117
]
118118
expect(output.string).to include(*expected_output)
119119
end

0 commit comments

Comments
 (0)