Posts Tagged ‘focus’

Focused tests with Phocus

Thursday, June 11th, 2009

If you ever comment out big chunks of your test suite so you can concentrate
on a single test, or use your editor’s ability to run the test under your
cursor, you’ve been focusing tests.

I make extensive use of this functionality myself, and though there are
already a few ways to do it (editor, autotest, …), none of them worked well
enough for true focusing (the editor is thrown off by contexts, etc).

So I finally put together a gem to do focus the way I needed it to work. To
use, simply call the focus method above the test you want focused.

gem install phocus --source http://gemcutter.org
require 'test/unit'
require 'phocus'

class TestWidget < Test::Unit::TestCase

  def test_foo
    assert false
  end

  focus
  def test_bar
    assert true
  end

  def test_baz
    assert false
  end
end

Running this suite will only call test_bar (and the suite will pass).

focus can be used on multiple tests, including across test suites.

class TestWidget < Test::Unit::TestCase

  focus
  def test_foo
  end

  def test_bar
  end
end

class TestGatget < Test::Unit::TestCase

  def test_abc
  end

  focus
  def test_def
  end
end

Running these will only call test_foo and test_def.

And Phocus isn’t affraid of fancy tests, either:

class TestUser < Test::Unit::TestCase

  context "Authentication" do
    test "should deny on wrong username" do
      ...
    end

    focus
    test "should deny on wrong password" do
      ...
    end
  end
end

will work just as well. In fact, Phocus is known to work with at least the
following testing frameworks:

Links

Source: http://github.com/mynyml/phocus
Issues: http://github.com/mynyml/phocus/issues