How to skip certain tests with Test::Unit
Asked Answered
F

3

9

In one of my projects I need to collaborate with several backend systems. Some of them somewhat lacks in documentation, and partly therefore I have some test code that interact with some test servers just to see everything works as expected. However, accessing these servers is quite slow, and therefore I do not want to run these tests every time I run my test suite.

My question is how to deal with a situation where you want to skip certain tests. Currently I use an environment variable 'BACKEND_TEST' and a conditional statement which checks if the variable is set for each test I would like to skip. But sometimes I would like to skip all tests in a test file without having to add an extra row to the beginning of each test.

The tests which have to interact with the test servers are not many, as I use flexmock in other situations. However, you can't mock yourself away from reality.

As you can see from this question's title, I'm using Test::Unit. Additionally, if it makes any difference, the project is a Rails project.

Filter answered 28/5, 2010 at 7:51 Comment(0)
W
8

New Features Of Test Unit 2.x suggests that test-unit 2.x (the gem version, not the ruby 1.8 standard library) allows you to omit tests.

Wendell answered 30/5, 2010 at 23:49 Comment(3)
Thank you! I actually read the slide before, but never looked if 2.x actually had been released. It has and I will take a closer look at it.Filter
Note that omitting tests fails them. If you don't want a bunch of failed tests, you could try Minitest's skip or just add a return statement on some condition at the top of your test.Turbulence
@Turbulence - Note: Omit raises an exception ONLY if no block is given. You can just pass the body of your test as the block.Octane
B
10

The features referred to in the previous answer include the omit() method and omit_if()

def test_omission
  omit('Reason')
  # Not reached here
end

And

def test_omission
  omit_if("".empty?)
  # Not reached here
end

From: http://test-unit.rubyforge.org/test-unit/en/Test/Unit/TestCaseOmissionSupport.html#omit-instance_method

Biancabiancha answered 3/4, 2014 at 1:3 Comment(0)
W
8

New Features Of Test Unit 2.x suggests that test-unit 2.x (the gem version, not the ruby 1.8 standard library) allows you to omit tests.

Wendell answered 30/5, 2010 at 23:49 Comment(3)
Thank you! I actually read the slide before, but never looked if 2.x actually had been released. It has and I will take a closer look at it.Filter
Note that omitting tests fails them. If you don't want a bunch of failed tests, you could try Minitest's skip or just add a return statement on some condition at the top of your test.Turbulence
@Turbulence - Note: Omit raises an exception ONLY if no block is given. You can just pass the body of your test as the block.Octane
C
1

I was confused by the following, which still raises an error to the console:

def test_omission
  omit('Reason')
  # Not reached here
end

You can avoid that by wrapping the code to skip in a block passed to omit:

def test_omission
  omit 'Reason' do
    # Not reached here
  end
end

That actually skips the test as expected, and outputs "Omission: Test Reason" to the console. It's unfortunate that you have to indent existing code to make this work, and I'd be happy to learn of a better way to do it, but this works.

Cerumen answered 3/11, 2022 at 19:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.