Read my latest article: Announcing RailsDeveloper (posted Wed, 01 Sep 2010 17:01:00 GMT)

Rails, Logger, and those pesky Tests

Posted by Robby Russell Wed, 25 Jan 2006 22:06:00 GMT

36 comments Latest by hedr dingfeng Wed, 25 Aug 2010 06:38:19 GMT

First of all, I would like to thank all of you who took a moment to gather around the campfire with me and share your stuff. That was much appreciated.

So, I started playing with the idea of logging in unit and functional tests and I quickly realized that calling logger.info 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.

You’re all in luck because:
  • A) my holster was eaten by my dog and
  • B) I found a quick solution to clean up this logger situation.

Okay… are you ready?

I know… this is totally groundbreaking!!!

Open up test/test_helper.rb and add the method… logger.

# other stuff at the top of the file... just 
# look down below at def logger
class Test::Unit::TestCase
  self.use_transactional_fixtures = true
  self.use_instantiated_fixtures  = false

  # here... look here! right below this
  def logger
    RAILS_DEFAULT_LOGGER
  end
end

Save that and pick up your pistol…

The next step is to call the logger method in your unit and functional tests.

def test_the_obvious
  logger.info( 'asserting that 1 is 1' )
  assert 1, 1
end

I posted this on Rails Weenie as well.

Subscribe to my RSS feed Enjoying the content? Be sure to subscribe to my RSS feed.
Comments

Leave a response

  1. Avatar
    Pat Wed, 25 Jan 2006 22:35:22 GMT

    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?

  2. Avatar
    Damien Tanner Thu, 26 Jan 2006 11:26:02 GMT

    My god the ground is breaking beneath my feet, only yesterday was I wondering how to do this.

  3. Avatar
    Mitch Fri, 27 Jan 2006 15:13:51 GMT

    I am also wondering why logging would be important in tests. I use $stdout.puts statements right now if I need to know anything.

    But that was a smart idea ;)

  4. Avatar
    Robby Russell Fri, 27 Jan 2006 16:49:54 GMT Recommend me on Working with Rails

    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. :-)

  5. Avatar
    logger Sun, 29 Jan 2006 15:16:43 GMT

    You want logging in unit tests so you can help debug what went wrong when a problem is found. Logging is for always.

  6. Avatar
    Victor Cosby Sat, 04 Feb 2006 03:22:32 GMT

    Sweet.

    Now you can also get your assert messages for failures to log by adding this to the end of your helper.

    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
    
  7. Avatar
    thankfully Mon, 13 Nov 2006 22:48:26 GMT

    Thanks a lot! Saved me a huge headache. Immediate use for the logger was to store a very long output string of a function so that I could write testcases for it.

  8. Avatar
    Waynebjp Tue, 14 Aug 2007 12:55:36 GMT

    http://adbrssgfsfrge.host.com desk3 [url=http://adbsssgfsfrge.host.com]desk4[/url] [link=http://adbassgfsfrge.host.com]desk6[/link]

  9. Avatar
    Satya Mon, 24 Sep 2007 13:05:43 GMT

    While this is cool and could be useful, one could also go the puts way and do this: rake test > log.log Yathink?

  10. Avatar
    sb Thu, 06 Dec 2007 20:37:04 GMT

    Satya, if you do that, your database statements and log messages will be in separate files. Not good. Personally, I find it a lot more useful when they’re in the same file so you can see the log messages and database statements in the order they were executed.

  11. Avatar
    Carl Humphrey Wed, 12 Dec 2007 11:36:47 GMT

    Thanks, solved a problem quickly with this bit of info

  12. Avatar
    Noah Stern Tue, 11 Nov 2008 01:53:15 GMT

    Thanks for this. I may be late to the thread party, but this is exactly what I was looking for.

  13. Avatar
    Jacolyte Fri, 05 Feb 2010 23:38:55 GMT

    Heed the statements of sb, for that is why you want to use Logger.

  14. Avatar
    karle Wed, 03 Mar 2010 20:36:19 GMT

    4 years later, this is still helping a brutha out. Thanks!

  15. Avatar
    ed hardy clothing Thu, 18 Mar 2010 06:39:00 GMT

    Thank you for your post!

  16. Avatar
    pandora jewelry Thu, 18 Mar 2010 07:57:48 GMT

    good sharing

  17. Avatar
    Christian Louboutin boots Wed, 24 Mar 2010 01:39:25 GMT
  18. Avatar
    Brandon Tue, 06 Apr 2010 20:46:22 GMT

    Great tip! I also just re-watched Ryan Bates’ The Logger screencast (http://railscasts.com/episodes/56-the-logger) and he pointed out we use the logger constant directly in the unit test with: RAILS_DEFAULT_LOGGER.debug “msg”

  19. Avatar
    Abercrombie Tue, 04 May 2010 07:07:26 GMT
  20. Avatar
    laptop battery manufacturer Thu, 13 May 2010 06:43:27 GMT

    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.

  21. Avatar
    supra suprano grey patent green shoes Thu, 27 May 2010 01:35:22 GMT
  22. Avatar
    ed hardy mens shoes Thu, 27 May 2010 01:35:46 GMT
  23. Avatar
    nike kobe 2k10 Sat, 05 Jun 2010 00:26:45 GMT

    ret re t

  24. Avatar
    mbt m.walk black Sat, 05 Jun 2010 00:27:11 GMT

    ytrytr y

  25. Avatar
    2010 germany 16 lahm Sat, 05 Jun 2010 06:37:32 GMT

    ty rt rt

  26. Avatar
    Supra Skylow 1.5 red Sat, 05 Jun 2010 06:37:43 GMT

    try tr

  27. Avatar
    air jordan 11 Wed, 30 Jun 2010 09:11:46 GMT

    Demonstrate a unique new conceptjordan shoesAwA78

  28. Avatar
    coach handbags Fri, 02 Jul 2010 00:38:36 GMT

    A Breitling Watches makes a complete man because the name is associated with elegance and status. It is a sweet poem on a wrist dial. All over the world busy professionals and achievers wear Breitling Aeromarine not only for its preciseness of time and speed movement but it also provides many utility services. Bentley 6.75 functionality is something beyond reproach. Breitling Windrider are generally larger that helps to display more functions and provide more visibility to the analog markers. Choosing hermes wisely: And, a real one from hermes belt comes with a key set and engraved lock. The markings of hermes wallet are embossed, and the designer will never put metal plates on its creations. Finally we need to check the material and the stitching of the handbag

  29. Avatar
    Louis Vuitton Wallets Mon, 19 Jul 2010 18:27:32 GMT

    Louis Vuitton Zippy Coin Purse M93739 Louis Vuitton Zippy Coin Purse Louis Vuitton Zippy Coin Purse M93741 Louis Vuitton Zippy Coin Purse Louis Vuitton Zippy Coin Purse M93740 Louis Vuitton Zippy Coin Purse Louis Vuitton Zippy Wallet M60241 Louis Vuitton Zippy Wallet Louis Vuitton Zippy Wallet M60242 Louis Vuitton Zippy Wallet Louis Vuitton Zippy Wallet M60243 Louis Vuitton Zippy Wallet Louis Vuitton Zippy Wallet M60244 Louis Vuitton Zippy Wallet Louis Vuitton Sarah Wallet M93742 Louis Vuitton Sarah Wallet Louis Vuitton Sarah Wallet M93743 Louis Vuitton Sarah Wallet Louis Vuitton Sarah Wallet M93744 Louis Vuitton Sarah Wallet Louis Vuitton Sarah Wallet M93745 Louis Vuitton Sarah Wallet Louis Vuitton Sarah Wallet M93746 Louis Vuitton Sarah Wallet Louis Vuitton Sarah Wallet M93747 Louis Vuitton Sarah Wallet Louis Vuitton Sarah Wallet M93748 Louis Vuitton Sarah Wallet Louis Vuitton Insolite Wallet M93749 Louis Vuitton Insolite Wallet Louis Vuitton Insolite Wallet M93750 Louis Vuitton Insolite Wallet Louis Vuitton Insolite Wallet M93751 Louis Vuitton Insolite Wallet Louis Vuitton Insolite Wallet M93752 Louis Vuitton Insolite Wallet Louis Vuitton Insolite Wallet M93753 Louis Vuitton Insolite Wallet Louis Vuitton Insolite Wallet M93754 Louis Vuitton Insolite Wallet Louis Vuitton Insolite Wallet M93755 Louis Vuitton Insolite Wallet Louis Vuitton Koala Wallet M58081 Louis Vuitton Koala Wallet Louis Vuitton Koala Wallet M58082 Louis Vuitton Koala Wallet Louis Vuitton Koala Wallet M58087 Louis Vuitton Koala Wallet Louis Vuitton Eugenie Wallet M93736 Louis Vuitton Eugenie Wallet Louis Vuitton Eugenie Wallet M93737 Louis Vuitton Eugenie Wallet Louis Vuitton Eugenie Wallet M93738Louis Vuitton Eugenie Wallet Louis Vuitton Keys Holder M93733 Louis Vuitton Keys Holder Louis Vuitton Keys Holder M93734 Louis Vuitton Keys Holder Louis Vuitton Keys Holder M93735 Louis Vuitton Keys Holder Louis Vuitton 4 Key Holder M93730 Louis Vuitton 4 Key Holder Louis Vuitton 4 Key Holder M93731 Louis Vuitton 4 Key Holder Louis Vuitton 4 Key Holder M93732 Louis Vuitton 4 Key Holder Louis Vuitton Business Card Holder M66559 Louis Vuitton Business Card Holder Louis Vuitton Business Card Holder M66560 Louis Vuitton Business Card Holder Louis Vuitton Business Card Holder M66561 Louis Vuitton Business Card Holder

  30. Avatar
    Hermes Birkin Tue, 27 Jul 2010 04:45:31 GMT
  31. Avatar
    Ed hardy womens Wed, 28 Jul 2010 02:15:27 GMT

    Ed hardy womens online store where various Ed hardy women’s swimwear, Ed hardy women’s T-shirts, Ed hardy women’s denim, Ed hardy women’s hoodies/outwear,Ed hardy women’s dresses,Ed hardy women’s shoes,Ed hardy women’s tanks,Ed hardy women’s boots,Ed hardy women’s bottoms,Ed hardy women’s lingerie,Ed hardy women’s suit. will be provided to you at the most competitive prices for your wearing pleasure. What’s more, all our Ed hardy products are the latest designs to meet your demands for fashion and individuality. So why not come here and have a look? You can always find the one that is most suitable for you

  32. Avatar
    Paul Smith Shoes Wed, 28 Jul 2010 02:17:19 GMT

    pual smith bag is fashion,convininet and is designed by the pual smith. In this side,we can offer to you many paul smith ,this bag can express the famouse characteristic of London of England.We can offer many style paul smith suits,if you are interest in them ,pls let me know .we will give you lower price ,fast delivery and best service.

    Paul Smith Shoes with our top quality,hot price,fast delivery and thoughtful customer service.Paul Smith Shoes played the part of still maintained its brief, but not simple style.Paul Smith is forever not out of the fashion world! When you are fond of some one of the Paul Smith Shoes in our web,now buy you can enjoy many benefits!Free shipping and at a sale!

  33. Avatar
    logo designs Fri, 30 Jul 2010 06:26:22 GMT

    Really I liked your post.How nicely you have completed your goals.Thanks for sharing it.

    logo design | Website design | logo designs | business logo

  34. Avatar
    bag manufacturer Fri, 06 Aug 2010 03:33:08 GMT

    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 pro

  35. Avatar
    http://www.bestretroshoes.com Tue, 10 Aug 2010 09:15:54 GMT
    Jordan Dub Zero,Jordan Dub Zero
    Jordan Spizike,Jordan Spizike
    Air Jordan 2,Air Jordan 2
    Air Jordan 3,Air Jordan 3
    Air Jordan 4,Air Jordan 4
    air jordan 6 rings,air jordan 6 rings
    jordan dub zero,jordan dub zero
    jordan sixty plus 60,jordan sixty plus 60
    air jordan l style,air jordan l style
    air jordan 19,air jordan 19
  36. Avatar
    hedr dingfeng Wed, 25 Aug 2010 06:38:19 GMT

    I am also wondering why logging would be important in tests. I use $stdout.puts statements right no

Share your thoughts... (really...I want to hear them)

Comments