# How to debug unit (and other) tests?

**URL:** <https://www.ruby-forum.com/t/how-to-debug-unit-and-other-tests/176882>\
**Category:** Rails\
**Created:** [October 7, 2009, 3:28pm UTC](https://www.ruby-forum.com/t/how-to-debug-unit-and-other-tests/176882 "2009-10-07T15:28:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [October 7, 2009, 3:28pm UTC](https://www.ruby-forum.com/t/how-to-debug-unit-and-other-tests/176882/1 "2009-10-07T15:28:57Z")

</div>

I’m sorry if this sounds like a totally newbie question, but…

Are there good tips and resources for debugging unit tests?  
Currently, I just fire up script/console using the “test” environment  
and just poke around randomly until I hit upon some inspiration. But  
I think there should be a better way, for example, it would be nice to  
single step through a particular test case using a debugger in one  
window and watching the test log in the other window.

Any tips?

Thanks.

–wpd

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [October 7, 2009, 5:33pm UTC](https://www.ruby-forum.com/t/how-to-debug-unit-and-other-tests/176882/2 "2009-10-07T17:33:43Z")

</div>

Patrick D. wrote:

> I’m sorry if this sounds like a totally newbie question, but…
> 
> Are there good tips and resources for debugging unit tests?  
> Currently, I just fire up script/console using the “test” environment  
> and just poke around randomly until I hit upon some inspiration. But  
> I think there should be a better way, for example, it would be nice to  
> single step through a particular test case using a debugger in one  
> window and watching the test log in the other window.
> 
> Any tips?

Just call the debugger from within your app code, or from the test  
itself. When the “debug” statement is executed, you’ll be in an  
interactive debugger. Simple.

> Thanks.
> 
> –wpd

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [October 7, 2009, 6:17pm UTC](https://www.ruby-forum.com/t/how-to-debug-unit-and-other-tests/176882/3 "2009-10-07T18:17:00Z")

</div>

On Wed, Oct 7, 2009 at 10:33 AM, Marnen Laibow-Koser  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Just call the debugger from within your app code, or from the test  
> itself. When the “debug” statement is executed, you’ll be in an  
> interactive debugger. Simple.

“debug” is for use with views, and does not stop execution.

\<%= debug @foo %\>

“debugger” is for actually invoking the debugger and stopping execution.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [October 7, 2009, 6:15pm UTC](https://www.ruby-forum.com/t/how-to-debug-unit-and-other-tests/176882/4 "2009-10-07T18:15:15Z")

</div>

On Wed, Oct 7, 2009 at 11:33 AM, Marnen Laibow-Koser  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Patrick D. wrote:
> 
> > Are there good tips and resources for debugging unit tests?
> 
> Just call the debugger from within your app code, or from the test  
> itself. When the “debug” statement is executed, you’ll be in an  
> interactive debugger. Simple.

I’m missing something because it is not simple…

class LotTest \< ActiveSupport::TestCase  
…  
test “add lot to assembly” do  
debug  
…  
end  
end

When I try running this as:

$ ruby -I test test/unit/lot\_test.rb -n test\_add\_lot\_to\_assembly

the test fails with:

NameError: undefined local variable or method `debug’ for …

When I try running this as:

$ rdebug -I test test/unit/lot\_test.rb -n test\_add\_lot\_to\_assembly  
(rdb:1) c

The test runs to completion and doesn’t stop at my #debug statement.

I won’t even go into my trials and tribulations trying to get this to  
run under emacs…

I agree though, this should really be simple. Having said that, I  
must be missing something terribly obvious.

–wpd

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [October 7, 2009, 7:19pm UTC](https://www.ruby-forum.com/t/how-to-debug-unit-and-other-tests/176882/5 "2009-10-07T19:19:40Z")

</div>

On Wed, Oct 7, 2009 at 12:45 PM, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 2009/10/7 Patrick D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > Are there good tips and resources for debugging unit tests?
> 
> Have a look at the rails guide on debugging. Particularly the section  
> on ruby-debug.

Thanks. That’s what I was looking for.

Now, if I could just get it to interact sanely with Emacs, I’d be a  
happy camper 🙂

–wpd

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [October 7, 2009, 6:46pm UTC](https://www.ruby-forum.com/t/how-to-debug-unit-and-other-tests/176882/6 "2009-10-07T18:46:52Z")

</div>

2009/10/7 Patrick D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I’m sorry if this sounds like a totally newbie question, but…
> 
> Are there good tips and resources for debugging unit tests?  
> Currently, I just fire up script/console using the “test” environment  
> and just poke around randomly until I hit upon some inspiration. Â But  
> I think there should be a better way, for example, it would be nice to  
> single step through a particular test case using a debugger in one  
> window and watching the test log in the other window.

Have a look at the rails guide on debugging. Particularly the section  
on ruby-debug.

Colin
