Unit testing of methods which do Kernel#exit

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 :slight_smile:

Regards,
             M.S.

···

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

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" <nobu.nokada@softhome.net> schrieb im Newsbeitrag
news:200412290040.iBT0e5jo004676@sharui.nakada.niregi.kanuma.tochigi.jp...

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

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" <lists+rubytalk@box.cz> schrieb im Newsbeitrag
news:20041229093518.GA9138@box.cz...

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

:slight_smile:

Kind regards

    robert