# Using assert outside of test unit

**URL:** <https://www.ruby-forum.com/t/using-assert-outside-of-test-unit/147296>\
**Category:** Ruby\
**Created:** [August 30, 2008, 2:45pm UTC](https://www.ruby-forum.com/t/using-assert-outside-of-test-unit/147296 "2008-08-30T14:45:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 30, 2008, 2:45pm UTC](https://www.ruby-forum.com/t/using-assert-outside-of-test-unit/147296/1 "2008-08-30T14:45:27Z")

</div>

hi is it possible to use assert in your actual code i.e. not just in the  
unit test but in say your class methods etc.

I tried to use it but ruby complains its not a recognised method for my  
class. (i made sure to write require ‘test/unit’ at the top of my class  
file.)

Unit testing is great for checking what methods return but Id also like  
to check whats going on inside sometimes. Having the program stop and  
tell me when something i didnt expect will also help to prevent bugs.

Is it possible. I googled and checked previous posts here but couldnt  
find anything.

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 30, 2008, 3:31pm UTC](https://www.ruby-forum.com/t/using-assert-outside-of-test-unit/147296/2 "2008-08-30T15:31:01Z")

</div>

Adam A. wrote:

> hi is it possible to use assert in your actual code i.e. not just in the  
> unit test but in say your class methods etc.
> 
> I tried to use it but ruby complains its not a recognised method for my  
> class. (i made sure to write require ‘test/unit’ at the top of my class  
> file.)
> 
> Unit testing is great for checking what methods return but Id also like  
> to check whats going on inside sometimes. Having the program stop and  
> tell me when something i didnt expect will also help to prevent bugs.
> 
> Is it possible. I googled and checked previous posts here but couldnt  
> find anything.

Those asserts are instance methods of Test::Unit::TestCase so you cannot  
use them outside the testcases.

But you could probably dynamically define your own class (or maybe there  
is one already) that holds an instance of Test::Unit::TestCase and  
redirects all assertions to it.

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 30, 2008, 3:47pm UTC](https://www.ruby-forum.com/t/using-assert-outside-of-test-unit/147296/3 "2008-08-30T15:47:18Z")

</div>

On Sat, Aug 30, 2008 at 2:40 PM, Adam A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## Is it possible. I googled and checked previous posts here but couldnt find anything.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

508/8 \> ruby -rtest/unit/assertions -e ‘include  
Test::Unit::Assertions; assert 41 \> 42’  
/usr/local/lib/ruby/1.8/test/unit/assertions.rb:48:in `assert_block': <false> is not true. (Test::Unit::AssertionFailedError) from /usr/local/lib/ruby/1.8/test/unit/assertions.rb:500:in `\_wrap\_assertion’  
from /usr/local/lib/ruby/1.8/test/unit/assertions.rb:46:in  
`assert_block' from /usr/local/lib/ruby/1.8/test/unit/assertions.rb:63:in `assert’  
from /usr/local/lib/ruby/1.8/test/unit/assertions.rb:495:in  
`_wrap_assertion' from /usr/local/lib/ruby/1.8/test/unit/assertions.rb:61:in `assert’  
from -e:1

## HTH Robert

[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

There’s no one thing that’s true. It’s all true.
