One of the most important aspects of test reports is that when something fails, you can easily debug what went wrong. Splitting tests into logical, small tests that exercise a single functionality in the system goes a long way toward this goal. Inevitably, you still occasionally get a weird failure where you have no idea what’s going wrong.
In such circumstances it’s vital to have sufficient information included in the test report to understand what happened — otherwise you’re stuck trying to do an autopsy without a corpse. When running Cucumber tests, we have found that a dump of the World is often extremely useful information.
The following piece of code does just that. Whenever a Cucumber scenario fails, it dumps the contents of the World to the console:
After do |scenario| if scenario.failed? Kernel.puts "Scenario failed, world contents:" vars = self.instance_variables.select { |v| !v.to_s.include?("__") } vars.sort.each do |v| value = instance_variable_get(v) Kernel.puts " #{v}=#{value}" end end end
This iterates through the variables and writes them out one by one. The select
statement on line 4 filters out some Cucumber-specific variables stored in the World.
You can easily customize this to output elsewhere (we’ve found Kernel.puts
to be the most reliable way to get the info out) or how to output certain data types. For example, we use the rest_client gem extensively, so we’ve customized the hook to detect the response object and to output value.code
, value.headers
and value.body
in that case.
