Skip to content

Commit

Permalink
Improve screenshot support (xcpretty#280)
Browse files Browse the repository at this point in the history
* Display screenshots next to tests

* Tidy report output

* Fix report output hiding tests

* Fix failing test

* Add support for screenshots containing suite name

* Fix failing test - had to shorten filename of screenshot

* Fix rubocop issue
  • Loading branch information
mattcotton authored and supermarin committed May 12, 2017
1 parent 8c3e0e2 commit 7a072a1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 28 deletions.
25 changes: 13 additions & 12 deletions assets/report.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
.test.passing { background-color: #CAF59F;}
.test.failing.odd { background-color: #EEC7CC;}
.test.passing.odd { background-color: #E5FBCF;}
.details { background-color: #F4DDE0; border: 1px solid #C84F5E;}
.details.failing { background-color: #F4DDE0; border: 1px solid #C84F5E;}
.details.passing { background-color: #E5F4DC; border: 1px solid #A1D761;}
.test .test-detail:last-child { padding-bottom: 8px;}
.test .title { float: left; font-size: 0.9em; margin-top: 8px; font-family: Menlo, Monaco, monospace;}
.test .time { float: left;margin: 4px 10px 0 20px;}
Expand Down Expand Up @@ -127,15 +128,6 @@
<h3 class="title"><%= name %></h3>
</section>
<section class="tests">
<% unless info[:screenshots].empty? %>
<div class="screenshots <%= info[:failing] ? 'failing' : 'passing'%>">
<% info[:screenshots].each_with_index do |screenshot, index| %>
<a href="javascript:toggleScreenshot('<%=name %>', <%=index %>)">
<img class="screenshot" id="screenshot-<%=name %>-<%=index %>" src="<%=screenshot %>" />
</a>
<% end %>
</div>
<% end %>
<table>
<% info[:tests].each_with_index do |test, index| %>
<% detail_class = test[:name].gsub(/\s/,'') %>
Expand All @@ -147,8 +139,8 @@
</td>
<td><h3 class="title"><%= test[:name] %></h3></td>
</tr>
<% if test[:reason] || test[:snippet] %>
<tr class="details <%= detail_class %>">
<% if test[:reason] || test[:snippet] || !test[:screenshots].empty? %>
<tr class="details <%= test[:failing] ? 'failing' : 'passing'%> <%= detail_class %>">
<td></td>
<td>
<% if test[:reason] %>
Expand All @@ -158,6 +150,15 @@
<section class="test-detail snippet"><%= test[:snippet] %></section>
<section class="test-detail"><%= test[:file] %></section>
<% end %>
<% if !test[:screenshots].empty? %>
<section class="test-detail">
<% test[:screenshots].each_with_index do |screenshot, idx| %>
<a href="javascript:toggleScreenshot('<%=test[:name] %>', <%=index %>)">
<img class="screenshot" id="screenshot-<%=test[:name] %>-<%=index %>" src="<%=screenshot %>" />
</a>
<% end %>
</section>
<% end %>
</td>
</tr>
<% end %>
Expand Down
28 changes: 15 additions & 13 deletions lib/xcpretty/reporters/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ def handle(line)
def format_failing_test(suite, test_case, reason, file)
add_test(suite, name: test_case, failing: true,
reason: reason, file: file,
snippet: formatted_snippet(file))
snippet: formatted_snippet(file),
screenshots: [])
end

def format_passing_test(suite, test_case, time)
add_test(suite, name: test_case, time: time)
add_test(suite, name: test_case, time: time, screenshots: [])
end

private
Expand All @@ -41,10 +42,9 @@ def formatted_snippet(filepath)
Syntax.highlight_html(snippet)
end


def add_test(suite_name, data)
@test_count += 1
@test_suites[suite_name] ||= {tests: [], screenshots: []}
@test_suites[suite_name] ||= {tests: []}
@test_suites[suite_name][:tests] << data
if data[:failing]
@test_suites[suite_name][:failing] = true
Expand All @@ -70,19 +70,21 @@ def load_screenshots
Dir.foreach(SCREENSHOT_DIR) do |item|
next if item == '.' || item == '..' || File.extname(item) != '.png'

suite = item.split(".")
next if suite.empty?

suite_name = find_test_suite(suite[0])
next if suite_name.nil?
test = find_test(item)
next if test.nil?

@test_suites[suite_name][:screenshots] << item
test[:screenshots] << item
end
end

def find_test_suite(image_name)
@test_suites.each do |key, value|
return key if image_name.start_with?(key.split('.').last)
def find_test(image_name)
@test_suites.each do |name, info|
info[:tests].each do |test, index|
combined_name = name + '_' + test[:name]
test_name_matches = image_name.start_with?(test[:name])
combined_name_matches = image_name.start_with?(combined_name)
return test if test_name_matches || combined_name_matches
end
end
nil
end
Expand Down
4 changes: 2 additions & 2 deletions spec/fixtures/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
)
SAMPLE_EXECUTED_TESTS = "Executed 4 tests, with 0 failures (0 unexpected) in 0.003 (0.004) seconds"
SAMPLE_SPECTA_EXECUTED_TESTS = " Executed 4 tests, with 0 failures (0 unexpected) in 10.192 (10.193) seconds"
SAMPLE_OCUNIT_TEST = "Test Case '-[RACCommandSpec enabled_signal_should_send_YES_while_executing_is_YES_and_allowsConcurrentExecution_is_YES]' passed (0.001 seconds)."
SAMPLE_OCUNIT_TEST = "Test Case '-[RACCommandSpec enabled_signal_should_send_YES_while_executing_is_YES]' passed (0.001 seconds)."
SAMPLE_SPECTA_TEST = " Test Case '-[SKWelcomeActivationViewControllerSpecSpec SKWelcomeActivationViewController_When_a_user_enters_their_details_lets_them_enter_a_valid_manager_code]' passed (0.725 seconds)."
SAMPLE_SLOWISH_TEST = "Test Case '-[RACCommandSpec enabled_signal_should_send_YES_while_executing_is_YES_and_allowsConcurrentExecution_is_YES]' passed (0.026 seconds)."
SAMPLE_SLOW_TEST = "Test Case '-[RACCommandSpec enabled_signal_should_send_YES_while_executing_is_YES_and_allowsConcurrentExecution_is_YES]' passed (0.101 seconds)."
Expand Down Expand Up @@ -555,7 +555,7 @@
warning: skipping copy phase strip, binary is code signed: /Users/dustin/Source/CocoaChip/build/Release/CocoaChipCore.framework/Versions/A/CocoaChipCore
)

SAMPLE_SCREENSHOT_FILE = 'RACCommandSpec, line 80, hello xcpretty.png'
SAMPLE_SCREENSHOT_FILE = 'RACCommandSpec_enabled_signal_should_send_YES_while_executing_is_YES.png'
SAMPLE_UNRELATED_IMAGE_FILE = 'apple_raw.png'

################################################################################
Expand Down
2 changes: 1 addition & 1 deletion spec/xcpretty/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ module XCPretty

it "parses passing ocunit tests" do
@formatter.should receive(:format_passing_test).with('RACCommandSpec',
'enabled_signal_should_send_YES_while_executing_is_YES_and_allowsConcurrentExecution_is_YES',
'enabled_signal_should_send_YES_while_executing_is_YES',
'0.001')
@parser.parse(SAMPLE_OCUNIT_TEST)
end
Expand Down

0 comments on commit 7a072a1

Please sign in to comment.