Skip to content

Commit

Permalink
Add feature tests for JSON formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
f-moya authored and PragTob committed Nov 29, 2020
1 parent a5d2871 commit 2226051
Show file tree
Hide file tree
Showing 4 changed files with 249 additions and 0 deletions.
43 changes: 43 additions & 0 deletions features/config_json_formatter.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
@test_unit @config
Feature:

SimpleCov::Formatter::JSONFormatter is one of the
formatters included by default, useful for exporting
coverage results in JSON format.

Background:
Given I'm working on the project "faked_project"
Scenario: With JSONFormatter
Given SimpleCov for Test/Unit is configured with:
"""
require 'simplecov'
require 'simplecov_json_formatter'
SimpleCov.formatter = SimpleCov::Formatter::JSONFormatter
SimpleCov.at_exit do
puts SimpleCov.result.format!
end
SimpleCov.start do
add_group 'Libs', 'lib/faked_project/'
end
"""

When I successfully run `bundle exec rake test`
Then a JSON coverage report should have been generated in "coverage"
And the output should contain "JSON Coverage report generated"

Scenario: When CC_TEST_REPORTER_ID is set in the environment
Given SimpleCov for Test/Unit is configured with:
"""
ENV['CC_TEST_REPORTER_ID'] = "9719ac886877886b7e325d1e828373114f633683e429107d1221d25270baeabf"
require 'simplecov'
SimpleCov.at_exit do
puts SimpleCov.result.format!
end
SimpleCov.start do
add_group 'Libs', 'lib/faked_project/'
end
"""

When I successfully run `bundle exec rake test`
Then a JSON coverage report should have been generated in "coverage"
And the output should contain "JSON Coverage report generated"
9 changes: 9 additions & 0 deletions features/step_definitions/json_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

Then /^the JSON coverage report should map:/ do |expected_report|
cd(".") do
json_report = File.open("coverage/coverage.json").read
expected_report = ERB.new(expected_report).result(binding)
expect(json_report).to eq(expected_report)
end
end
10 changes: 10 additions & 0 deletions features/step_definitions/simplecov_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@
)
end

Then /^a JSON coverage report should have been generated(?: in "([^"]*)")?$/ do |coverage_dir|
coverage_dir ||= "coverage"
steps %(
Then the output should contain "Coverage report generated"
And a directory named "#{coverage_dir}" should exist
And the following files should exist:
| #{coverage_dir}/coverage.json |
)
end

Then /^no coverage report should have been generated(?: in "([^"]*)")?$/ do |coverage_dir|
coverage_dir ||= "coverage"
steps %(
Expand Down
187 changes: 187 additions & 0 deletions features/test_unit_basic.feature
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,190 @@ Feature:

And the report should be based upon:
| Unit Tests |

Scenario:
Given SimpleCov for Test/Unit is configured with:
"""
ENV['CC_TEST_REPORTER_ID'] = "9719ac886877886b7e325d1e828373114f633683e429107d1221d25270baeabf"
require 'simplecov'
SimpleCov.start
"""

When I open the coverage report generated with `bundle exec rake test`
Then I should see the groups:
| name | coverage | files |
| All Files | 91.38% | 6 |

And I should see the source files:
| name | coverage |
| lib/faked_project.rb | 100.00 % |
| lib/faked_project/some_class.rb | 80.00 % |
| lib/faked_project/framework_specific.rb | 75.00 % |
| lib/faked_project/meta_magic.rb | 100.00 % |
| test/meta_magic_test.rb | 100.00 % |
| test/some_class_test.rb | 100.00 % |

# Note: faked_test.rb is not appearing here since that's the first unit test file
# loaded by Rake, and only there test_helper is required, which then loads simplecov
# and triggers tracking of all other loaded files! Solution for this would be to
# configure simplecov in this first test instead of test_helper.

And the report should be based upon:
| Unit Tests |

And a JSON coverage report should have been generated
And the JSON coverage report should map:
"""
{
"meta": {
"simplecov_version": "<%= SimpleCov::VERSION %>"
},
"coverage": {
"<%= Dir.pwd %>/lib/faked_project.rb": {
"lines": [
null,
null,
1,
1,
1,
null,
null,
null,
5,
3,
null,
null,
1
]
},
"<%= Dir.pwd %>/lib/faked_project/framework_specific.rb": {
"lines": [
null,
null,
null,
null,
null,
1,
1,
1,
0,
null,
null,
1,
0,
null,
null,
1,
1,
null,
null,
null
]
},
"<%= Dir.pwd %>/lib/faked_project/meta_magic.rb": {
"lines": [
null,
null,
1,
1,
1,
1,
null,
null,
null,
1,
1,
1,
null,
null,
null,
1,
1,
1,
null,
1,
1,
1,
null,
null,
null,
null
]
},
"<%= Dir.pwd %>/lib/faked_project/some_class.rb": {
"lines": [
null,
null,
1,
1,
1,
null,
1,
2,
null,
null,
1,
1,
null,
null,
1,
1,
1,
null,
0,
null,
null,
0,
null,
null,
1,
null,
1,
0,
null,
null
]
},
"<%= Dir.pwd %>/test/meta_magic_test.rb": {
"lines": [
null,
null,
1,
null,
1,
1,
1,
null,
null,
1,
1,
1,
1,
null,
null
]
},
"<%= Dir.pwd %>/test/some_class_test.rb": {
"lines": [
null,
null,
1,
null,
1,
1,
2,
null,
null,
1,
1,
null,
null,
1,
1,
null,
null
]
}
}
}
"""

0 comments on commit 2226051

Please sign in to comment.