From: Robert Klemme Date: 2010-04-22T21:44:52+09:00 Subject: Re: Test::Unit examples and OptionParser 2010/4/22 Martin Hansen : > I am looking for a stack of unit tests examples to check the behavior of > OptionParser for parsing command line options, such as: > > > [option]: >  --help >  --output_file >  --cutoff_value >  --list_of_keys >  --verbose What exactly do you expect? Do you want to test whether OptionParser works properly or do you want to test your option spec? For the former you'll likely find tests where OptionParser sources are. For the latter you will have to provide different argument lists to OP and see whether it works as intended, e.g. require 'optparse' def my_parse(argv) options = {} OptionParser.new do |opts| opts.on '--help' do |val| options[:help] = val end end.parse! argv options end [ [[], [], {}], [%w{--help}, [], {:help => true}], ].each do |inp, outp, opts| printf "%-30s argv=%p\n", "before", inp options = my_parse inp printf "%-30s argv=%p options=%p\n", "after", inp, options puts "options ok #{opts == options}", "argv ok #{outp == inp}" end Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/