How to run one Test::Unit test case from vim

November 28, 2009

I recently updated my vim script for running rspec specs from vim.

I’ve been working on a project that has a large Test::Unit test suite and I missed being able to execute one testcase from within vim. I finally got around to coding this. HItting !s will execute the current testcase based on the cursor’s position. Hitting !S will run all the cases in the file. It’s intelligent about whether you’re in a Test::Unit file or an rspec file. (It will also run your scala specs if you’re in an sbt project.)

function! BDD(args)
 if bufname("%") =~ "test.rb"
   call RunTest(a:args)
 elseif bufname("%") =~ ".scala"
   call RunSBTTest()
 elseif bufname("%") =~ "spec.rb"
   call RunSpec(a:args)
 else
   echo "don't know how to BDD this file"
 end
endfunction
 
function! RunTest(args)
  let cursor = matchstr(a:args, '\d\+')
  if cursor
    while !exists("cmd") && cursor != 1
      if match(getline(cursor), 'def test') >= 0
        let cmd = ":! ruby % -vv -n ". matchstr(getline(cursor), "test_[a-zA-Z_]*")
      else
        let cursor -= 1
      end
    endwhile
  end
  if !exists("cmd")
    let cmd = ":! ruby % -vv"
  end
  execute cmd
endfunction
 
function! RunSpec(args)
  if exists("b:rails_root") && filereadable(b:rails_root . "/script/spec")
    let spec = b:rails_root . "/script/spec"
  else
    let spec = "spec"
  end
  let cmd = ":! " . spec . " % -cfn " . a:args
  execute cmd
endfunction
 
function! RunSBTTest()
  execute ":! java -jar ~/sbt-launcher-0.5.5.jar test"
endfunction
 
map !s :call BDD("-l " . <C-r>=line('.')<CR>)
map !S :call BDD("")
 
view raw bdd.vim This Gist brought to you by GitHub.