unit testing of methods which do Kernel#exit

Discussion in 'Ruby' started by Michal, Dec 28, 2004.

  1. Michal

    Michal Guest

    Hi,

    I ran into a problem -- how to unit test methods that have
    Kernel#exit call in case of troubles (fatal errors).

    I found solution (imho) better than changing behaviour
    of Kernel.exit -- I simply raise SystemExit, exitcode ...

    But because I'm not sure about correctness of this "replacement",
    I'd like to ask more experienced people here whether I miss
    something in the "big picture" or there really are no hidden
    monsters that will bite me after some time of doing this.

    If you have any comment, I'd appreciate it :)

    Regards,
    M.S.
    --
    # Michal Safranek, email:
    a=(("a".."z").to_a+["@","."]);p(("%b"%[0x645bbb83a6a496]
    ).scan(/...../).map{|x|a[Integer("0b"+x)]}.join.reverse)
     
    Michal, Dec 28, 2004
    #1
    1. Advertising

  2. Hi,

    At Wed, 29 Dec 2004 07:56:16 +0900,
    Michal wrote in [ruby-talk:124685]:
    > I ran into a problem -- how to unit test methods that have
    > Kernel#exit call in case of troubles (fatal errors).
    >
    > I found solution (imho) better than changing behaviour
    > of Kernel.exit -- I simply raise SystemExit, exitcode ...


    Kernel.exit does it too, so you can catch "exit" by `rescue
    SystemExit'.

    --
    Nobu Nakada
     
    Nobuyoshi Nakada, Dec 29, 2004
    #2
    1. Advertising

  3. "Nobuyoshi Nakada" <> schrieb im Newsbeitrag
    news:...
    > Hi,
    >
    > At Wed, 29 Dec 2004 07:56:16 +0900,
    > Michal wrote in [ruby-talk:124685]:
    > > I ran into a problem -- how to unit test methods that have
    > > Kernel#exit call in case of troubles (fatal errors).
    > >
    > > I found solution (imho) better than changing behaviour
    > > of Kernel.exit -- I simply raise SystemExit, exitcode ...

    >
    > Kernel.exit does it too, so you can catch "exit" by `rescue
    > SystemExit'.


    Apart from that I would not write tests that do exit the system. Usually
    tests fail with an exception - that should be enough.

    robert
     
    Robert Klemme, Dec 29, 2004
    #3
  4. Michal

    Michal Guest

    Hi,

    > Apart from that I would not write tests that do exit the system.
    > Usually tests fail with an exception - that should be enough.


    my tests use just assert_* ... but the tested code sometimes exits
    using Kernel#exit [well, now I raise SystemExit].

    I have to add that after Nobu's remark I also tried to
    assert_raises(SystemExit) while using Kernel#exit and it also
    works well. The thing is that now I find "raise SystemExit" more
    adequate[*].

    Regards,
    Michal

    Ref: [*]: eh, is this the correct word to convey following in English:
    "I like it better and the code seems more 'clear' to me when
    using raise"?
    --
    # Michal Safranek, email:
    a=(("a".."z").to_a+["@","."]);p(("%b"%[0x645bbb83a6a496]
    ).scan(/...../).map{|x|a[Integer("0b"+x)]}.join.reverse)
     
    Michal, Dec 29, 2004
    #4
  5. "Michal" <> schrieb im Newsbeitrag
    news:...
    > Hi,
    >
    > > Apart from that I would not write tests that do exit the system.
    > > Usually tests fail with an exception - that should be enough.

    >
    > my tests use just assert_* ... but the tested code sometimes exits
    > using Kernel#exit [well, now I raise SystemExit].


    Ah, ok. So you're probably not in the position to change the tested code?
    If so, I still prefer to use an exception in favour of direct termination.

    > I have to add that after Nobu's remark I also tried to
    > assert_raises(SystemExit) while using Kernel#exit and it also
    > works well. The thing is that now I find "raise SystemExit" more
    > adequate[*].
    >
    > Regards,
    > Michal
    >
    > Ref: [*]: eh, is this the correct word to convey following in English:
    > "I like it better and the code seems more 'clear' to me when
    > using raise"?


    Disclaimer: I'm not a native speaker.

    Well, "adequate" is a far more general term. I think I understood what
    you meant although "adequate" maybe lacks the notion of your personal
    feelings ("like it better"). I think the term is more factual. You could
    have also written "I prefer 'raise SystemExit' because it's cleaner".
    Just a suggestion...

    :)

    Kind regards

    robert
     
    Robert Klemme, Dec 29, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Elhanan
    Replies:
    1
    Views:
    329
  2. yogesh
    Replies:
    3
    Views:
    590
    Kenny McCormack
    Feb 12, 2006
  3. Ulrich Eckhardt

    unit-profiling, similar to unit-testing

    Ulrich Eckhardt, Nov 16, 2011, in forum: Python
    Replies:
    6
    Views:
    336
    Roy Smith
    Nov 18, 2011
  4. Replies:
    3
    Views:
    178
  5. Bill Mosteller
    Replies:
    0
    Views:
    230
    Bill Mosteller
    Oct 22, 2009
Loading...

Share This Page