Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I test only a method with Ruby's Test::Unit?

Imagine I have a test like this:

class MyUnitTest < Test::Unit::TestCase
  def test_first
    # test code here
  end

  def test_second
    # test code here
  end

  def test_third
    # test code here
  end
end

My test cases are destructive, and I need to regenerate the input in between tests. Therefore, it would be useful to run only one test case at a time. Currently, my approach is to comment tests that I don't want executed, but surely there must be a better way?

So, for example, how do I run only test_first when I execute my test?

like image 304
Geo Avatar asked Mar 04 '26 17:03

Geo


2 Answers

Use --name PATTERN argument in order to filter out test names you want to run.

D:\Projects>ruby test.rb
Loaded suite test
Started
...
Finished in 0.000000 seconds.

3 tests, 0 assertions, 0 failures, 0 errors, 0 skips

Test run options: --seed 39768

D:\Projects>ruby test.rb -n test_first
Loaded suite test
Started
.
Finished in 0.000000 seconds.

1 tests, 0 assertions, 0 failures, 0 errors, 0 skips

Test run options: --seed 53891 --name "test_first"
like image 71
DNNX Avatar answered Mar 07 '26 10:03

DNNX


Can you regenerate the input with ruby? Then you could use the setup-method.

gem 'test-unit', '>= 2.1.1' #startup
require 'test/unit'

class Test_setup < Test::Unit::TestCase
  def setup
    #Your regeneration of the input
    puts "Setup"
  end

  def teardown
    #Clean actions
    puts "End"
  end

  def test_1()
    puts "Testing setup 1"
  end      
end
like image 38
knut Avatar answered Mar 07 '26 11:03

knut