# Testing for warnings

**URL:** <https://www.ruby-forum.com/t/testing-for-warnings/108320>\
**Category:** Ruby\
**Created:** [July 2, 2007, 9:11am UTC](https://www.ruby-forum.com/t/testing-for-warnings/108320 "2007-07-02T09:11:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![oscar-alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e36b37/32.png) [@oscar-alexander](https://www.ruby-forum.com/u/oscar-alexander)\
**Post date:** [July 2, 2007, 9:11am UTC](https://www.ruby-forum.com/t/testing-for-warnings/108320/1 "2007-07-02T09:11:03Z")

</div>

Is there a way of asserting that a warning is raised in a unit test?  
So I could write something like:

assert\_warns “Blah blah” do function\_that\_warns(foo) end

I can’t seem to find anything in the test/unit docs, but it seems  
like something that would be relatively common to test for…

Alex G.

Bioinformatics Center  
Kyoto University

---

<div class="post-metadata">

**Author:** ![oscar-alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e36b37/32.png) [@oscar-alexander](https://www.ruby-forum.com/u/oscar-alexander)\
**Post date:** [July 2, 2007, 11:24pm UTC](https://www.ruby-forum.com/t/testing-for-warnings/108320/2 "2007-07-02T23:24:20Z")

</div>

On 7/2/07, Alex G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there a way of asserting that a warning is raised in a unit test?  
> So I could write something like:
> 
> assert\_warns “Blah blah” do function\_that\_warns(foo) end
> 
> I can’t seem to find anything in the test/unit docs, but it seems  
> like something that would be relatively common to test for…

You may try to write that yourself by replacing Kernel#warn, though  
this way you cannot check warnings from C code. Alternatively you  
could replace/capture $stderr/STDERR and look for warnings there.

---

<div class="post-metadata">

**Author:** ![oscar-alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e36b37/32.png) [@oscar-alexander](https://www.ruby-forum.com/u/oscar-alexander)\
**Post date:** [July 3, 2007, 1:46am UTC](https://www.ruby-forum.com/t/testing-for-warnings/108320/3 "2007-07-03T01:46:37Z")

</div>

On 3 Jul 2007, at 06:23, Jano S. wrote:

> this way you cannot check warnings from C code. Alternatively you  
> could replace/capture $stderr/STDERR and look for warnings there.

I specifically need to check for warnings from C code so replacing  
Kernel#warn won’t work. I will try hooking into STDERR though -  
thanks for the idea.

Alex G.

Bioinformatics Center  
Kyoto University

---

<div class="post-metadata">

**Author:** ![oscar-alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e36b37/32.png) [@oscar-alexander](https://www.ruby-forum.com/u/oscar-alexander)\
**Post date:** [July 3, 2007, 1:53am UTC](https://www.ruby-forum.com/t/testing-for-warnings/108320/4 "2007-07-03T01:53:37Z")

</div>

On 7/2/07, Alex G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I specifically need to check for warnings from C code so replacing  
> Kernel#warn won’t work. I will try hooking into STDERR though -  
> thanks for the idea.

Possibly make use of Mocha or another mocking framework?

class Foo  
def bar  
warn “baz”  
end  
end

require “test/unit”  
require “rubygems”  
require “mocha”

class TestFoo \< Test::Unit::TestCase

def test\_bar\_should\_warn  
a = Foo.new  
a.expects(:warn).with(“baz”)  
a.bar  
end

end
