New Bamboo Web Development

Bamboo blog. Our thoughts on web technology.

Cucumber Tags: For Fun & Profit

by Oliver Legg

Recently Cucumber added support for tagging features and scenarios which allows you to flexibly organise and categorise your features.

We've been able to use these tags to speed up our tests, quite considerably in some places. Once of our projects is using Sphinx (with the brilliant Thinking Sphinx), to search the database. As Cucumber tests the full stack, in order to get accurate test results we need to configure, index and start Sphinx before running the test suite.

Using tags, we need to only start up Sphinx if the features have been tagged "@sphinx". Whilst this doesn't give us any speed improvements when running the whole test suite, it does give measurably better results (in the order of 10 - 15 seconds on my machine) when only running a single test which doesn't need Sphinx.

 1 # Cucumber::Rails::World.use_transactional_fixtures
 3 Before do
 4   DatabaseCleaner.clean
 5 end
 7 def sphinx_task(task)
 8   %x{RAILS_ENV=test `which rake` ts:#{task}}
 9 end
11 Before('@sphinx') do
12   unless ThinkingSphinx.sphinx_running?
13     sphinx_task(:config)
14     sphinx_task(:index)
15     sphinx_task(:run)
16   end
17 end
19 at_exit do
20   DatabaseCleaner.clean
22   if ThinkingSphinx.sphinx_running?
23     sphinx_task(:stop)
24   end
25 end

As an aside, it is important to note, that because we are using Sphinx we need to disable transactional fixtures for the indexing to work properly. There is the excellent database_cleaner gem which can wipe your database between steps, which you can see above.