Robby on Rails: Rails, Logger, and those pesky Teststhoughts.sort_by{|t| t[:topic]}.collect tag:www.robbyonrails.com,2005:TypoTypo2006-09-05T22:12:47-04:00Robby Russellurn:uuid:3e1240ab-de42-4ef0-b436-7de4a28f1efe2006-01-25T16:06:00-05:002006-09-05T22:12:47-04:00Rails, Logger, and those pesky Tests<p>First of all, I would like to thank all of you who took a moment to <a href="http://www.robbyonrails.com/articles/2006/01/09/request-for-rails-debugging-tools">gather around the campfire with me</a> and share <a href="http://www.robbyonrails.com/articles/2006/01/09/request-for-rails-debugging-tools#comments">your stuff</a>. That was much appreciated.</p>
<p><img src="http://www.planetargon.com/files/~robby/cowboy_kids.jpg" style="float: right;" ></p>
<p>So, I started playing with the idea of logging in unit and functional tests and I quickly realized that calling <code>logger.info</code> wasn’t an option. What’s the deal-e-o? Before I gather up my posse and conspire a train hijacking, I decided that I would see how quickly I could solve my problem before I called you all out to the Rio Rails Grande for an old-fashioned shoot out. Ya know, the kind that results in a Bon Jovi song and a little blood.</p>
You’re all in luck because:
<ul>
<li>A) my holster was eaten by my dog
and</li>
<li>B) I found a quick solution to clean up this <code>logger</code> situation.</li>
</ul>
<p>Okay… are you ready?</p>
<p>I know… this is <strong>totally groundbreaking</strong>!!!</p>
<p>Open up <code>test/test_helper.rb</code> and add the method… <code>logger</code>.</p>
<div class="typocode"><pre><code class="typocode_ruby "><span class="comment"># other stuff at the top of the file... just </span>
<span class="comment"># look down below at def logger</span>
<span class="keyword">class </span><span class="class">Test::Unit::TestCase</span>
<span class="constant">self</span><span class="punct">.</span><span class="ident">use_transactional_fixtures</span> <span class="punct">=</span> <span class="constant">true</span>
<span class="constant">self</span><span class="punct">.</span><span class="ident">use_instantiated_fixtures</span> <span class="punct">=</span> <span class="constant">false</span>
<span class="comment"># here... look here! right below this</span>
<span class="keyword">def </span><span class="method">logger</span>
<span class="constant">RAILS_DEFAULT_LOGGER</span>
<span class="keyword">end</span>
<span class="keyword">end</span></code></pre></div>
<p>Save that and pick up your pistol…</p>
<p>The next step is to call the logger method in your unit and functional tests.</p>
<div class="typocode"><pre><code class="typocode_ruby "><span class="keyword">def </span><span class="method">test_the_obvious</span>
<span class="ident">logger</span><span class="punct">.</span><span class="ident">info</span><span class="punct">(</span> <span class="punct">'</span><span class="string">asserting that 1 is 1</span><span class="punct">'</span> <span class="punct">)</span>
<span class="ident">assert</span> <span class="number">1</span><span class="punct">,</span> <span class="number">1</span>
<span class="keyword">end</span></code></pre></div>
<p>I <a href="http://rails.techno-weenie.net/tip/2005/11/20/log_within_tests">posted this</a> on <a href="http://rails.techno-weenie.net">Rails Weenie</a> as well.</p><p>First of all, I would like to thank all of you who took a moment to <a href="http://www.robbyonrails.com/articles/2006/01/09/request-for-rails-debugging-tools">gather around the campfire with me</a> and share <a href="http://www.robbyonrails.com/articles/2006/01/09/request-for-rails-debugging-tools#comments">your stuff</a>. That was much appreciated.</p>
<p><img src="http://www.planetargon.com/files/~robby/cowboy_kids.jpg" style="float: right;" ></p>
<p>So, I started playing with the idea of logging in unit and functional tests and I quickly realized that calling <code>logger.info</code> wasn’t an option. What’s the deal-e-o? Before I gather up my posse and conspire a train hijacking, I decided that I would see how quickly I could solve my problem before I called you all out to the Rio Rails Grande for an old-fashioned shoot out. Ya know, the kind that results in a Bon Jovi song and a little blood.</p>
You’re all in luck because:
<ul>
<li>A) my holster was eaten by my dog
and</li>
<li>B) I found a quick solution to clean up this <code>logger</code> situation.</li>
</ul>
<p>Okay… are you ready?</p>
<p>I know… this is <strong>totally groundbreaking</strong>!!!</p>
<p>Open up <code>test/test_helper.rb</code> and add the method… <code>logger</code>.</p>
<div class="typocode"><pre><code class="typocode_ruby "><span class="comment"># other stuff at the top of the file... just </span>
<span class="comment"># look down below at def logger</span>
<span class="keyword">class </span><span class="class">Test::Unit::TestCase</span>
<span class="constant">self</span><span class="punct">.</span><span class="ident">use_transactional_fixtures</span> <span class="punct">=</span> <span class="constant">true</span>
<span class="constant">self</span><span class="punct">.</span><span class="ident">use_instantiated_fixtures</span> <span class="punct">=</span> <span class="constant">false</span>
<span class="comment"># here... look here! right below this</span>
<span class="keyword">def </span><span class="method">logger</span>
<span class="constant">RAILS_DEFAULT_LOGGER</span>
<span class="keyword">end</span>
<span class="keyword">end</span></code></pre></div>
<p>Save that and pick up your pistol…</p>
<p>The next step is to call the logger method in your unit and functional tests.</p>
<div class="typocode"><pre><code class="typocode_ruby "><span class="keyword">def </span><span class="method">test_the_obvious</span>
<span class="ident">logger</span><span class="punct">.</span><span class="ident">info</span><span class="punct">(</span> <span class="punct">'</span><span class="string">asserting that 1 is 1</span><span class="punct">'</span> <span class="punct">)</span>
<span class="ident">assert</span> <span class="number">1</span><span class="punct">,</span> <span class="number">1</span>
<span class="keyword">end</span></code></pre></div>
<p>I <a href="http://rails.techno-weenie.net/tip/2005/11/20/log_within_tests">posted this</a> on <a href="http://rails.techno-weenie.net">Rails Weenie</a> as well.</p>
Jacolyteurn:uuid:eac0080c-907f-4296-8808-a72a3c7c24732010-02-05T17:38:55-05:002010-02-06T07:39:12-05:00Comment on Rails, Logger, and those pesky Tests by Jacolyte<p>Heed the statements of sb, for that is why you want to use Logger.</p>Victor Cosbyurn:uuid:32109ad8-7d5d-43ab-ac25-d3bcb11a46792006-02-03T21:22:32-05:002006-09-05T22:12:53-04:00Comment on Rails, Logger, and those pesky Tests by Victor Cosby<p>Sweet.</p>
<p>Now you can also get your assert messages for failures to log by adding this to the end of your helper.</p>
<pre>
module Test
module Unit
module Assertions
alias_method :assert_block_original, :assert_block
def assert_block(message="assert_block failed.", &block)
begin
assert_block_original(message, &block)
rescue AssertionFailedError => error
logger.info(message.to_s)
raise error
end
end
end
end
end
</pre>loggerurn:uuid:58e4241e-5943-4278-819e-771db9bb641b2006-01-29T09:16:43-05:002006-09-05T22:12:53-04:00Comment on Rails, Logger, and those pesky Tests by logger<p>You want logging in unit tests so you can help debug what went wrong when a problem is found. Logging is for always.</p>Robby Russellurn:uuid:756e72f1-e69f-43ba-9572-ca91864053262006-01-27T10:49:54-05:002006-09-05T22:12:53-04:00Comment on Rails, Logger, and those pesky Tests by Robby Russell<p>I find that when I’m testing some more complex units of test and want to get some information about what is happening before and after each database interaction… having some of my own text help break things out gives me clarity. :-)</p>Mitchurn:uuid:f90e7f04-10b4-470d-91fa-fe69c75eccad2006-01-27T09:13:51-05:002006-09-05T22:12:53-04:00Comment on Rails, Logger, and those pesky Tests by Mitch<p>I am also wondering why logging would be important in tests. I use $stdout.puts statements right now if I need to know anything.</p>
<p>But that was a smart idea ;) </p>Damien Tannerurn:uuid:157120f2-9e29-48d4-a2a0-de07ae0b960a2006-01-26T05:26:02-05:002006-09-05T22:12:53-04:00Comment on Rails, Logger, and those pesky Tests by Damien Tanner<p>My god the ground is breaking beneath my feet, only yesterday was I wondering how to do this.</p>Paturn:uuid:001f5a6a-499d-4d08-862a-733ebd55a1a72006-01-25T16:35:22-05:002006-09-05T22:12:51-04:00Comment on Rails, Logger, and those pesky Tests by Pat<p>Maybe you posted this in another blog, but I’m curious as to why you’d want logging in unit tests. It’s not like you’re getting diagnostic info from your app in development mode that you don’t want filling up your production logs..simple puts statements should do the trick in unit tests, shouldn’t they?</p>