From: Caleb Clausen Date: 2010-05-11T01:01:56+09:00 Subject: Re: Unit Test of method calling system() - how? On 5/10/10, Martin Hansen wrote: > How can I unit test the two methods: > > print_usage_full_and_exit > > and > > print_usage_short_and_exit > > http://pastie.org/953804 > > Possibly, I need to change the code into something more testable! > > Ideas most welcome! I would extract a helper from both methods which does the bulk of the work (the first 2 lines) but does not exit. Then test the helper and don't bother testing the methods that call it. You then have the problem that this print_wiki command is printing something to stdout, which you want to test. Either capture stdout in the test and verify it contains what you want, or use backticks (`...`) instead of system in your helper method. Since print_wiki might change its behavior, you might think about making the assertion that tests print_wiki's output string somewhat loose.... just verify that some expected substring occurs in there somewhere, rather than verifying that it is exactly equal to some expected string. $0 is also going to present you some problems in a test... it will not be the same value in the test as it is when run from your main program. Not sure what to suggest about that without more context. 3 other points: looks like both methods have an unintended extra trailing + on their first line standard style is to use 2 spaces for indentation; you appear to be using 8 since the 2 methods are nearly identical, you should pull out the common code.