unit testing of methods which do Kernel#exit

M

Michal

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

Nobuyoshi Nakada

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

Robert Klemme

Nobuyoshi Nakada said:
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
 
M

Michal

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"?
 
R

Robert Klemme

Michal said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top