A small gem to lubricate your testing workflow with prettier than usual diffs.

Enable fancy colored diffs in your minitest assertions
https://github.com/angelkar/pretty-diffs

1. Motivation

When you make assertions between large strings with Minitest, for example JSON responses, it is laborious to identify what has changed. The usual workflow involves copy-pasting the output into a diff tool; a rather boring and time-consuming process.

Minitest chooses to compare strings using diff, which is line-oriented. However, 99% of the time, we do not intend to compare lines, we want just the words.

PrettyDiffs is a tool that supports this desire by overriding the default Minitest method, and relieves our workflow.

2. Installation

2.1 Install wdiff on your system:

Wdiff is a wrapper around diff, it works by creating two temporary files, one word per line, and then executes diff on these files. It collects the diff output and uses it to produce a nicer display of word differences between the original files.

# mac
$ brew install wdiff 

# linux
$ apt get-install wdiff 

2.2 Install the PrettyDiffs module:

Add this line to your application’s Gemfile:

group :test do
  gem 'pretty-diffs'
end

And then execute:

$ bundle

Or install it yourself as:

$ gem install pretty-diffs

2.3 How to use

Include the module in the Minitest classes to trigger the pretty diffs:

require 'pretty_diffs'

module ActiveSupport

  class TestCase
    include PrettyDiffs # add this line

  end
end

Reference

Enable fancy colored diffs in your minitest assertions
https://github.com/angelkar/pretty-diffs

打赏

Leave a Reply

Your email address will not be published. Required fields are marked *