Ruby on Rails
HowtoRunOneTest

This came up on IRC.

You can run only one of your test methods using the -n switch:

ruby test/unit/your_test -n test_name


Which will run a unit test. You can also run a functional test by using:

ruby test/functional/your_test -n test_name

You can even do

ruby test/funtional/your_test -n /stuff/

then it’ll run all the methods with stuff in the name

If you are running emacs, here is a hack to bind this to F8:


(require 'which-func)
(setq imenu-auto-rescan t)       ; ensure function names auto-refresh
(setq imenu-max-item-length 200) ; ensure function names are not truncated
(defun ruby-execute-current-file ()
  "Execute the current ruby file (e.g. to execute all tests)."
  (interactive)
  (compile (concat "ruby " (file-name-nondirectory (buffer-file-name)))))
(defun ruby-test-function ()
  "Test the current ruby function (must be runnable via ruby <buffer> --name <test>)."
  (interactive)
  (let* ((funname (which-function))
         (fn (and funname (and (string-match "\\(#\\|::\\)\\(test.*\\)" funname) (match-string 2 funname)))))
    (compile (concat "ruby " (file-name-nondirectory (buffer-file-name)) (and fn (concat " --name " fn))))))

; run the current buffer using Shift-F8
(add-hook 'ruby-mode-hook (lambda () (local-set-key [S-f8] 'ruby-execute-current-file)))
; run the current test function using F8 key
(add-hook 'ruby-mode-hook (lambda () (local-set-key [f8] 'ruby-test-function)))

——

See HowtoRakeOneTest