# How to interrupt unit tests if a library is not present

**URL:** <https://www.ruby-forum.com/t/how-to-interrupt-unit-tests-if-a-library-is-not-present/171261>\
**Category:** Ruby\
**Created:** [June 29, 2009, 5:54pm UTC](https://www.ruby-forum.com/t/how-to-interrupt-unit-tests-if-a-library-is-not-present/171261 "2009-06-29T17:54:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Diana\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@Diana\_J](https://www.ruby-forum.com/u/Diana_J)\
**Post date:** [June 29, 2009, 5:54pm UTC](https://www.ruby-forum.com/t/how-to-interrupt-unit-tests-if-a-library-is-not-present/171261/1 "2009-06-29T17:54:12Z")

</div>

Hi all,

I am developing support for PhyloXML in BioRuby as Google Summer of Code  
project. I have a question about unit testing.

My code depends on libxml-ruby library (other parts of BioRuby does  
not). So  
I want to test if libxml-ruby is required and then if it is not, i want  
to  
quit my unit test suite (a file tes\_phyloxml.rb) nicely (don’t perform  
any  
unit tests related to phyloxml since they anyways gonna fail) but the  
rest  
of unit test suites should continue running.

Right now the it completely exits, if can’t load xml library, but I  
would  
like the other tests to continue running.

#First let’s test if xml library is here, since it will be required by  
bio/db/phyloxml  
begin  
require ‘xml’  
rescue LoadError  
puts "Please install libxml-ruby library. It is needed for  
Bio::PhyloXML  
module. Unit tests will exit now.  
exit 1  
end

require ‘bio/db/phyloxml’

class TestPhyloXML1 \< Test::Unit::TestCase

```
 def setup
  @phyloxml = Bio::PhyloXML.new(TestPhyloXMLData.example_xml)
end

def test_init
  assert_equal(@phyloxml.class, Bio::PhyloXML)
end

```

#[…]

end

Thanks,

Diana

---

<div class="post-metadata">

**Author:** ![Diana\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@Diana\_J](https://www.ruby-forum.com/u/Diana_J)\
**Post date:** [June 29, 2009, 6:10pm UTC](https://www.ruby-forum.com/t/how-to-interrupt-unit-tests-if-a-library-is-not-present/171261/2 "2009-06-29T18:10:39Z")

</div>

Hi –

On Tue, 30 Jun 2009, Diana J. wrote:

> exit 1
> 
> def test\_init  
> assert\_equal(@phyloxml.class, Bio::PhyloXML)  
> end
> 
> #[…]
> 
> end

Maybe something like this, if it’s not too convoluted:

no\_xml = false

begin  
require ‘xml’  
rescue LoadError  
puts “No libxml; not running those tests…” # or whatever  
no\_xml = true  
end

unless no\_xml  
class TestPhyloXML1

etc.

David

---

<div class="post-metadata">

**Author:** ![Diana\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@Diana\_J](https://www.ruby-forum.com/u/Diana_J)\
**Post date:** [June 29, 2009, 6:42pm UTC](https://www.ruby-forum.com/t/how-to-interrupt-unit-tests-if-a-library-is-not-present/171261/3 "2009-06-29T18:42:52Z")

</div>

Thanks a lot! It worked like a charm!

Diana

---

<div class="post-metadata">

**Author:** ![Diana\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@Diana\_J](https://www.ruby-forum.com/u/Diana_J)\
**Post date:** [June 29, 2009, 10:08pm UTC](https://www.ruby-forum.com/t/how-to-interrupt-unit-tests-if-a-library-is-not-present/171261/4 "2009-06-29T22:08:24Z")

</div>

On Tue, 30 Jun 2009, David A. Black wrote:

> begin  
> require “xml”  
> rescue LoadError  
> puts “No xml library…”  
> else  
> class TestPhyloXML1 \< Test::Unit::TestCase  
> …  
> end  
> end

OK, third time a charm.

begin  
require ‘xml’  
class TestPhyloXML1 \< Test::Unit::TestCase  
…  
end  
rescue LoadError  
puts “No xml library…”  
end

I think that’s as compact and streamlined as I can get it 🙂

David

---

<div class="post-metadata">

**Author:** ![Diana\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@Diana\_J](https://www.ruby-forum.com/u/Diana_J)\
**Post date:** [June 29, 2009, 9:44pm UTC](https://www.ruby-forum.com/t/how-to-interrupt-unit-tests-if-a-library-is-not-present/171261/5 "2009-06-29T21:44:04Z")

</div>

Hi –

On Tue, 30 Jun 2009, Diana J. wrote:

> Thanks a lot! It worked like a charm!

Rob B., in another context, reminded me that  
exception-handling blocks can have an “else” clause. So you could do  
the slightly simpler:

begin  
require “xml”  
rescue LoadError  
puts “No xml library…”  
else  
class TestPhyloXML1 \< Test::Unit::TestCase  
…  
end  
end

David

---

<div class="post-metadata">

**Author:** ![Diana\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@Diana\_J](https://www.ruby-forum.com/u/Diana_J)\
**Post date:** [June 30, 2009, 1:58am UTC](https://www.ruby-forum.com/t/how-to-interrupt-unit-tests-if-a-library-is-not-present/171261/6 "2009-06-30T01:58:25Z")

</div>

On Mon, Jun 29, 2009 at 4:07 PM, David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > exception-handling blocks can have an “else” clause. So you could do  
> > end  
> > puts “No xml library…”  
> > Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
> > Now available: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://manning.com/black2))  
> > “Ruby 1.9: What You Need To Know” Envycasts with David A. Black  
> > [http://www.envycasts.com](http://www.envycasts.com)

Yes indeed! Thanks!

Diana

---

<div class="post-metadata">

**Author:** ![Diana\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@Diana\_J](https://www.ruby-forum.com/u/Diana_J)\
**Post date:** [June 30, 2009, 7:46pm UTC](https://www.ruby-forum.com/t/how-to-interrupt-unit-tests-if-a-library-is-not-present/171261/7 "2009-06-30T19:46:19Z")

</div>

Diana J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > Thanks a lot! It worked like a charm!  
> > > else  
> > > class TestPhyloXML1 \< Test::Unit::TestCase  
> > > …  
> > > end  
> > > rescue LoadError  
> > > puts “No xml library…”  
> > > end
> > 
> > I think that’s as compact and streamlined as I can get it 🙂

Indeed - quite a while ago I wrote myself a utility for requiring  
without penalty in case of failure, and here it is (as you’ll see, it  
looks like a mere generalization of what David Black has already shown):

def myrequire(\*what)  
catch NameError  
what.each do |thing|  
begin  
require((t = Array(thing))[0])  
Array(t[1]).each {|inc| include self.class.const\_get(inc)}  
rescue LoadError  
puts "Failed to locate required "#{thing}". "  
puts $!  
end  
end  
end

Here are some usage notes:

(1) no penalty for failure; we catch the LoadError and we don’t re-raise

(2) arg can be an array, so multiple requires can be combined in one  
line

(3) array element itself can be a pair, in which case second must be  
array of desired includes as symbols; that way, we don’t try to perform  
the includes unless the require succeeded (and if an include fails, that  
does raise all the way since we don’t catch NameError)

So, you might say e.g.

myrequire “pathname”, “yaml”, “erb”, “pp”, “uri”, “rubygems”, “exifr”

Or (an example with an include):

myrequire [‘rexml/document’, :REXML]

m.
