A long-lived project that still receives updates
["Adds Minitest assertion for testing HTML output, including contents, within a provided string"]
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Runtime

~> 5.7, >= 5.7.0
 Project Readme

Minitest::HaveTag

Ruby - Gem Version - Minitest Style Guide

Coverage: 100%

Adds methods to Minitest to test for existence of HTML tags, including contents, within a provided string.


Installation

Add this gem to your application's Gemfile:

gem 'minitest-have_tag'

And then execute bundle install.

Or install it yourself with:

gem install minitest-have_tag

Usage

Setup

Add the gem in your spec/spec_helper.rb file after the 'minitest/autorun' gem to automatically add the assertion methods.

require 'minitest/autorun'
require 'minitest/have_tag'
# ...

Usage with default assertions

class MyTest < Minitest::Test
  def test_basic_html_tag
    html = '<br>'
    assert_have_tag(html, 'br')
  end

  def test_basic_html_attributes
    html = '<div id="header" class="flex"></div>'

    # CSS class paths
    assert_have_tag(html, 'div.flex')
    assert_have_tag(html, 'div[@class=flex]')

    assert_have_tag(html, 'div#header')
  end

  # check for contents within a <div...>
  def test_basic_html_tag_contents
    html = '<div class="row">contents</div>'

    assert_have_tag(html, 'div.row', 'contents')
  end

  def test_complex_html_with_contents
    html = <<~HTML
      <div id="intro" class="row">
        <div class="col-md-12">
          <h1>Header</h1>
        </div>
      </div>
    HTML

    assert_have_tag(html, 'div#intro.row > .col-md-12 > h1', 'Header')
  end

  # Error handling
  def test_basic_error_handling
    html = '<br>'
    error_msg = 'Expected "<br>" to have tag "div", but no such tag was found'

    assert_returns_error(error_msg) do
      assert_have_tag(html, 'div')
    end
  end

  ## Testing for non-existence of the expected HTML tag, including contents

  def test_negated_basic_html_tag
    html = '<abbr>'
    refute_have_tag(html, 'br')
  end

  def test_negated_basic_html_attributes
    html = '<div id="header" class="flex"></div>'

    refute_have_tag(html, 'div.flex-wrap')
    refute_have_tag(html, 'div[@class=flex-wrap]')

    refute_have_tag(html, 'div#hero')
  end

  def test_negated_basic_html_tag_contents
    html = '<div class="row">contents</div>'

    refute_have_tag(html, 'div.row', 'some content')
  end

  def test_negated_complex_html_with_contents
    html = <<~HTML
      <div id="intro" class="row">
        <div class="col-md-12">
          <h1>Header</h1>
        </div>
      </div>
    HTML

    refute_have_tag(html, 'div#intro.row > .col-lg-12 > h2', 'Header')
  end

  def test_negated_basic_error_handling
    html = '<br>'
    error_msg = 'Expected "<br>" to NOT have tag "br", but such a tag was found'

    assert_returns_error(error_msg) do
      refute_have_tag(html, 'br')
    end
  end
end


Using Expectation Syntax

describe 'HTML Response' do
  let(:html) { '<div id="header" class="flex"></div>' }
  let(:label) { %(<label for="name">Username:</label>\n) }

  it 'confirms HTML attributes' do
    _(html).must_have_tag('div.flex')
  end

  it 'refutes HTML attribute' do
    _(html).wont_have_tag('div.flex-wrap')
  end

  it 'regex content matching' do
   _(label).must_have_tag('label[for=name]', /User/) }

   _(label).wont_have_tag('label[for=name]', /Name/) }
  end
  # ...
end

Development

Get started with these commands:

# clone the repository
git clone https://github.com/kematzy/minitest-have_tag.git

# change into repository directory
cd minitest-have_tag/

# install dependencies
bundle install

# run the default tests with coverage & rubocop checking
bundle exec rake

# enable automatic reloading of tests during development
bundle exec guard

You can also run bin/console for an interactive prompt that will allow you to experiment.

Install locally

To install this gem onto your local machine, run bundle exec rake install.

Release new version

To release a new version:

  • ensure bundle exec rake passes.
  • update the version number in version.rb.
  • run bundle exec rake release which:
    • creates a Git tag for the version;
    • pushes Git commits and tags to GitHub;
    • pushes the .gem file to RubyGems.org.

Contributing

Bug reports and pull requests are welcome at github.com/kematzy/minitest-have_tag.

Copyright

Copyright (c) 2015 - 2025 Kematzy

The gem is available as open source under the terms of the MIT License.