# Aborting unit tests

**URL:** <https://www.ruby-forum.com/t/aborting-unit-tests/130946>\
**Category:** Ruby\
**Created:** [March 4, 2008, 6:27pm UTC](https://www.ruby-forum.com/t/aborting-unit-tests/130946 "2008-03-04T18:27:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [March 4, 2008, 6:27pm UTC](https://www.ruby-forum.com/t/aborting-unit-tests/130946/1 "2008-03-04T18:27:11Z")

</div>

Hi all,

I’ve come across this pattern a few times now, and I’m wondering what  
the best approach is. Say I’ve got a class like this:

class A  
def public\_method  
c(b(a))  
end

private  
def a  
# a stuff  
end

def b(foo)  
# b stuff  
end

def c(bar)  
# c stuff  
end  
end

Now, in my unit tests, I’m fine with having separate tests for a, b and  
c, but I still want to test public\_method separately, to ensure that  
they’re strung together properly. However, often I can test before  
calling it that, for instance, a precondition for b() isn’t met, because  
of breakage somewhere else in the codebase. In that case, I don’t want  
to log public\_method as having failed - that’s just noise, the fact that  
I’ve got a failure will be logged when the test for b() itself fails.  
I’d like to be able to write this in my test:

class ATest \< Test::Unit::TestCase  
def test\_public\_method  
abort\_if(!precondition\_for\_a ||  
!precondition\_for\_b ||  
!precondition\_for\_c)  
assert(A.new.public\_method, “It didn’t work!”)  
end  
end

I don’t want to just wrap the assertion in a conditional, because then  
it wouldn’t be reported (and a reduced assertion count is too easy to  
miss) - but I specifically don’t want it reported as a failure.  
Ideally, I’d like to see results like this:

$ ruby test/a\_test.rb  
Loaded suite test/a\_test.rb  
Started

# failure report here

…a…a…F  
Finished in 0.092032 seconds.

19 tests, 48 assertions, 1 failure, 0 errors, 2 aborts

Has anyone looked at this before? I can’t see anything in the test/unit  
docs that look relevant. Is it worth looking at, or is it just  
indicative of some code that needs cleaning up on my part? If so, how  
should it be fixed? As a concrete example, in Rails controller tests I  
often find myself doing something like:

get :foo  
assert\_response :success  
assert\_not\_nil assigns(:bar)

where if the assert\_response :success fails, I know the reason will be  
caught in a different test that checks the various error states, but  
it’s not worth checking assigns(:bar) in this test because it can’t  
possibly be valid.

Any hints gratefully received…
