Sunday, November 30, 2008

Finding which rspec tests produce unexpected outputs

You know running hundred of tests should only show '.', 'F', or 'E' and nothing more. You feel disturbed when some passing tests print debugging statements or rspec matcher complains that one of erb/rhtml views do not have a closing html tag and print a bunch of html outputs.

You want to find which tests among hundreds produce these but you have no clue where to find them. You don't want to search for 'puts', 'p' or 'inspect' from the entire codebase. Nor would you want to find which erb/rhtml files have missing closing tags. What should you do?

My colleague, Toby, suggested me to look at rspec source code, specifically at the formatters so that we can find a spot that runs examples and shows the '.' output. So I did and found the progress_bar_formatter.rb in lib/spec/runner/formatter.

def example_passed(example)
@output.print green('.')

We can put a print statement to identify which example (rspec test) is passing. Either @output.print example.description or @output.print example.inspect should be fine. Once we know the example names, we should be able to find where they are. Now we can fix the problem and make all test outputs produce those little green dots.

No comments: